Last active
March 19, 2019 03:00
-
-
Save theKashey/fce216c1595d1900ed829936fb5d0cde to your computer and use it in GitHub Desktop.
ReactClassMemoize using https://github.com/theKashey/kashe
This file contains hidden or 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 Example extends Component { | |
| state = { | |
| filterText: "", | |
| }; | |
| // ******************************************************* | |
| // NOTE: this example is NOT the recommended approach. | |
| // See the examples below for our recommendations instead. | |
| // ******************************************************* | |
| static getDerivedStateFromProps(props, state) { | |
| // Re-run the filter whenever the list array or filter text change. | |
| // Note we need to store prevPropsList and prevFilterText to detect changes. | |
| if ( | |
| props.list !== state.prevPropsList || | |
| state.prevFilterText !== state.filterText | |
| ) { | |
| return { | |
| prevPropsList: props.list, | |
| prevFilterText: state.filterText, | |
| filteredList: props.list.filter(item => item.text.includes(state.filterText)) | |
| }; | |
| } | |
| return null; | |
| } | |
| handleChange = event => { | |
| this.setState({ filterText: event.target.value }); | |
| }; | |
| render() { | |
| return ( | |
| <Fragment> | |
| <input onChange={this.handleChange} value={this.state.filterText} /> | |
| <ul>{this.state.filteredList.map(item => <li key={item.id}>{item.text}</li>)}</ul> | |
| </Fragment> | |
| ); | |
| } | |
| } |
This file contains hidden or 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
| import {kashe} from "kashe"; | |
| const filter = kashe( | |
| (list, filterText) => list.filter(item => item.text.includes(filterText)) | |
| ); | |
| class Example extends Component { | |
| state = { | |
| filterText: "", | |
| }; | |
| static getDerivedStateFromProps(props, state) { | |
| if ( | |
| props.list !== state.prevPropsList || | |
| state.prevFilterText !== state.filterText | |
| ) { | |
| return { | |
| prevPropsList: props.list, | |
| prevFilterText: state.filterText, | |
| filteredList: memoizedFilterFunction(props.list, state.filterText) | |
| }; | |
| } | |
| return null; | |
| } | |
| handleChange = event => { | |
| this.setState({ filterText: event.target.value }); | |
| }; | |
| render() { | |
| return ( | |
| <Fragment> | |
| <input onChange={this.handleChange} value={this.state.filterText} /> | |
| <ul>{this.state.filteredList.map(item => <li key={item.id}>{item.text}</li>)}</ul> | |
| </Fragment> | |
| ); | |
| } | |
| } |
This file contains hidden or 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
| import {kashe} from "kashe"; | |
| const filter = kashe( | |
| (list, filterText) => list.filter(item => item.text.includes(filterText)) | |
| ); | |
| class Example extends Component { | |
| // State only needs to hold the current filter text value: | |
| state = { filterText: "" }; | |
| handleChange = event => { | |
| this.setState({ filterText: event.target.value }); | |
| }; | |
| render() { | |
| // Calculate the latest filtered list. If these arguments haven't changed | |
| // since the last render, `kashe` will reuse the last return value. | |
| const filteredList = filter(this.props.list, this.state.filterText); | |
| return ( | |
| <Fragment> | |
| <input onChange={this.handleChange} value={this.state.filterText} /> | |
| <ul>{filteredList.map(item => <li key={item.id}>{item.text}</li>)}</ul> | |
| </Fragment> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment