Этот метод стоит на событие onChange.
handleChange = ({ target: { value, dataset: { type } } }) => {
if (value.length <= limits.user.max) {
this.setState({
[type]: value,
});
}
}
<input onChange={this.handleChange} data-type="xxx">
<input onChange={this.handleChange} data-type="yyy">
<input onChange={this.handleChange} data-type="zzz">
Но как мне их передать в дочку на отображение ?
<SearchHub error={error} isLoaded={isLoaded} items={items} />
class SearchHub extends Component {
render(){
const { error, isLoaded, items } = this.props;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <Loader type="big" active> </Loader>;
} else {
return (
<div className={styles.parent}>
<div>
{items.map(item => (
<Gapped gap={20} key={item.name}>
<span><b>{item.rating.totalLove}</b></span>
<span><img src={shape} alt="heart"/></span>
<span><img src={item.avatarUri} alt="avatar" className={styles.photo}/></span>
<span>{item.name}<br/>{item.role}</span>
</Gapped>
))}
</div>
</div>
);
}
}
}
class Parent extends Component {
state = {
search: '',
data: [],
};
componentDidMount() {
fetchData().then(({ data }) => this.setState({ data }));
}
handleFilterChange = e => {
this.setState({ search: e.target.value });
};
render() {
const { data, search } = this.state;
const filteredData = data.filter(el => /* some stuff */);
return (
<Wrapper>
<SearchFilter onChange={this.handleFilterChange} />
<DataList data={filteredData} />
</Wrapper>
)
}
}
handleInputChange(e) {
let inputValue = e.target.value;
clearTimeout(this.timeout);
setTimeout(_ => this.fetchUsers(inputValue), 1000);
}
fetchUsers(value) {
this.setState({isLoaded: false});
axios.get(`url_to_get_users_by_name`, {param: value})
.then((response) => response.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
.catch(error => console.log('parsing failed', error))
}