Answer the question
In order to leave comments, you need to log in
How to arrange the interaction of React components in an existing project?
To begin with, I will give a very exaggerated example of what is already there, to make it easier to understand what I want to get.
The first component does the input of numbers:
class FirstСomponent extends React.Component {
render() {
return <input type="number" onChange={this.props.onChange} />;
}
}
class SecondСomponent extends React.Component {
render() {
return <div>{this.props.data.val}</div>;
}
}
class Main extends React.Component {
state = { val: 0 };
onChange = e => {
this.setState({ val: e.target.value });
};
render() {
return (
<div>
<FirstСomponent onChange={this.onChange} />
<SecondСomponent data={this.state} />
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Main />, rootElement);
Answer the question
In order to leave comments, you need to log in
If someone suddenly needs the same thing, here is a good example (not mine)
{
...
render() {
const { dateStr } = this.state;
return (
<React.Fragment>
{ReactDOM.createPortal(
<HeaderContent dateStr={dateStr} />,
document.getElementById("header")
)}
<div>This is the main content</div>
{ReactDOM.createPortal(
<Footer text="this is cool footer text" />,
document.getElementById("footer")
)}
</React.Fragment>
);
}
}
ReactDOM.render(<MyApp />, document.getElementById("main"));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question