I have a table that displays contacts and I want to sort the contacts by first name. The contacts array comes from the redux store, which will come then come through the pro
Your approach to use both redux store and local store is correct.
Just do not try to duplicate the state from redux store in your component. Keep referring to it via props.
Instead create a sortedContacts
function that computes value on the fly by applying locally-stored sortBy
param to redux-stored contacts.
const Table extends React.Component {
constructor(props) {
super(props);
this.state = {
sortBy: 'id' // default sort param
}
}
sortContacts(param) {
this.setState({ sortBy: param})
}
sortedContacts() {
return this.props.contacts.sort(...); // return sorted collection
}
render() {
return (
this.sortContacts("firstName")}>First Name
{this.sortedContacts()}
)
}
}