Passing states up
Unanswered
Japanese jack mackerel posted this in #help-forum
Japanese jack mackerelOP
I have a form comprised of 2+ components, within each component the useStates are being declared. when the wrapping components submit button is clicked all the components need to send their data up, and im not sure how
i know i coould pass a function down for each component to call to pass data up but im not sure how to trigger that on each component
i know i coould pass a function down for each component to call to pass data up but im not sure how to trigger that on each component
8 Replies
Declare the necessary state(s) in the wrapping component and call the setter function in the child components or use context if you feel it better suits your use case
@Plague Declare the necessary state(s) in the wrapping component and call the setter function in the child components or use context if you feel it better suits your use case
Japanese jack mackerelOP
idk what use context is, but its a fair amount of states so if feasible id prefer to keep the use states seperated to avoid having to pass all the setters down
@Japanese jack mackerel idk what use context is, but its a fair amount of states so if feasible id prefer to keep the use states seperated to avoid having to pass all the setters down
Yeah context sounds like exactly what you need, you should learn about it. https://react.dev/learn/passing-data-deeply-with-context
Japanese jack mackerelOP
ill give it a read, tyty
Japanese jack mackerelOP
@Plague so would i make the context variable part of useEffect in the component so if it changes it triggers something?
otherwise im still not seeing how i could do something at the wrapping component to trigger the data lifting during runtime
@Japanese jack mackerel <@683517071749021779> so would i make the context variable part of useEffect in the component so if it changes it triggers something?
Yeah, just be cautious if you are setting state in a effect that it doesn’t depend on that state variable to avoid an infinite render loop
@Plague Yeah, just be cautious if you are setting state in a effect that it doesn’t depend on that state variable to avoid an infinite render loop
Japanese jack mackerelOP
yeah id do a seperate useeffect just for the context variable and just make it like a bool that i flip to send the trigger to all the cub components