Created
October 2, 2019 12:25
-
-
Save alonbardavid/9e773adac2f59a7a2e4203969fdf155f to your computer and use it in GitHub Desktop.
Patterns for deriving state gist2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SortedList extends React.Component { | |
onFilterChange = (event)=>{ | |
const filter = event.target.value; | |
this.setState({ | |
filter, | |
}) | |
} | |
sortByKey = (column) =>{ | |
this.setState({ | |
sortKey:column | |
}) | |
} | |
sortList(){ | |
const {list} = this.props; | |
const {filter,sortKey} = this.state; | |
const filteredList = list.filter(item=>item.text.indexOf(filter)>=0) | |
return list.sort((a,b)=>a[column]>b[column]?1:a[column] < b[column]?-1:0) | |
} | |
render(){ | |
const {filter} = this.props; | |
const sortedList = this.sortList(); | |
return <div> | |
{sortedList.map(item=> | |
<ItemView item={item} key={item.id} /> | |
)} | |
<button onClick={()=>this.sortByKey("columnA")}>Sort by A</button> | |
<button onClick={()=>this.sortByKey("columnB")}>Sort by B</button> | |
<input type="text" value={filter} | |
onChange={this.onFilterChange}>Sort by A</button> | |
</div> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment