Following is my code (which is working fine) in which I am able to sort list on the basis of input provided in text box. In constructor
method I declared my states like this -
this.state = {
data: ["Adventure", "Romance", "Comedy", "Drama"],
tableData: []
};
In componentDidMount
method I assign the data
key state in tableData
.
componentDidMount() {
this.setState({
tableData: this.state.data
});
}
My question is - Is it the correct way of doing this as somehow I myself not feeling confident about this code quality (Initializing the tableData as []
and then setting tableData: this.state.data
in componentDidMount
method ). Let me know if I can improve this also what will change if I fetch
the data from an API which is the best place to initialize and use in an app.
Working Code Example - https://codesandbox.io/s/k9j86ylo4o
Code -
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: ["Adventure", "Romance", "Comedy", "Drama"]
};
this.handleChange = this.handleChange.bind(this);
}
refineDataList(inputValue) {
const listData = this.state.data;
const result = listData.filter(item =>
item.toLowerCase().match(inputValue.toLowerCase())
);
this.setState({
data: result
});
}
handleChange(e) {
const inputValue = e && e.target && e.target.value;
this.refineDataList(inputValue);
}
render() {
return (
<div className="App">
<h3>DATA SEARCH</h3>
<div className="form">
<input type="text" onChange={this.handleChange} />
</div>
<div className="result">
<ul>
{this.state.data &&
this.state.data.map((item, i) => {
return <li key={i}>{item}</li>;
})}
</ul>
</div>
</div>
);
}
}