Answer the question
In order to leave comments, you need to log in
How to wire React components?
Hello.
The site has two components, let's say a button and text, when you click on the button, the data array for the text should change. But the problem is that these components cannot be crammed into one and thus use setState. How to decide?
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Such tasks are easily solved through the parent:
class Parent extends Component {
state = {
text: 'some text',
};
handleClick = () => {
this.setState({ text: 'some other text'});
};
render() {
const { text } = this.state;
return (
<Wrapper>
<Button onClick={this.handleClick} />
<Text>{text}</Text>
</Wrapper>
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question