Answer the question
In order to leave comments, you need to log in
How to pass data from bottom to top using React's Context API?
Hey!
I'm trying to implement Context Api in my project. And now I'm trying to understand: is it possible to pass data from bottom to top , i.e. from child components to parent or from Consumers to Provider ?
Example:
React.createContext
export const eventData = {
topic: "defaultTopic",
};
export const CreateEventFormContext = React.createContext(eventData);
valueFromProvider
in any way. Consumer
But I want to pass an attribute value value
that changes dynamically using handleChange
in Provider
. Well, then from Provider
I plan to transfer data to ControlForm.jsx and from there I will send an array of data to the database. ...
handleChange(event) {
this.setState({ value: event.target.value });
}
...
<CreateEventFormContext.Consumer>
{valueFromProvider => (
<input type="text" value={this.state.value} onChange={this.handleChange} />
)}
</CreateEventFormContext.Consumer>
<CreateEventFormContext.Provider value="HELLO WORLD">
<Topic />
</CreateEventFormContext.Provider>
HELLO WORLD
to valueFromProvider
in my Consumer
. But how can I do the same in reverse? When I started to delve into it, Context Api
I thought that this would save me from callbacks, but so far, apart from the callback, I don’t see any other option to transfer data up. Answer the question
In order to leave comments, you need to log in
The Context API is used to pass context, not to manage state. Read about Redux .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question