Next.js Discord

Discord Forum

Triggering state update in sibling component

Answered
Australian Cattle Dog posted this in #help-forum
Open in Discord
Australian Cattle DogOP
I have a page where there are two rich text editors and one form.
When a user clicks the submit button, I want to extract the data from the rich text editors and send it to a database.

I am having a hard time figuring out how best to do this. I have managed to come up with a solution using useState and useEffect, but it feels like a suboptimal solution.
How can I best handle state management in this situation?


Current code
const [context, setContext] = useState<string>('');
  const [question, setQuestion] = useState<string>('');
  const [answers, setAnswers] = useState<string>('');
  const [submitted, setSubmitted] = useState<boolean>(false);

  // Callback function for richtext editor 1
  const handleContextSubmit = (data: object) => {
    setContext(JSON.stringify(data));
  }
  // Callback function for richtext editor 2
  const handleQuestionSubmit = (data: object) => {
    setQuestion(JSON.stringify(data));
  }
  // Callback function for input form
  const handleAnswersSubmit = (data: object) => {
    setAnswers(JSON.stringify(data));
  }
  const handleSubmit = () => {
    setSubmitted(true);
  }

  useEffect(() => {
    // submit data to the server - using server actions?
    console.log(context);
    console.log(question);
    setSubmitted(false);
  }, [context, question, answers]);
Answered by Jboncz
You can also just hoist the state and pass the setters and getters.
View full answer

12 Replies

If its just triggering a function, you could do some callback shenanigans but I believe its not going to be the prettiest of solutions
Btw you dont need to wrap everything in context provider, only the parts in the tree that are going to use the context
Australian Cattle DogOP
Ok thanks a lot!
@Clown If it involves syncing state, useContext is pretty much one of the best solutions
Australian Cattle DogOP
I have updated the original question to include a code snippet.
Hmm.. not sure if you solved this but that's not really the context api. If that works for you good if it doesn't here's a whole guide on the topic by react itself:
https://react.dev/learn/passing-data-deeply-with-context
You can also just hoist the state and pass the setters and getters.
Answer
Remember you can pass functions down to child components, so if you hoist whatever call you need to the aprent and send it down as a prop you can access it across the parent child boundary
<AddRoleDialog isOpen={dialogAddRoleOpen} setIsOpen={setDialogAddRoleOpen} type='exception' />


Very simple example of this. When the dialog is open, it needs to be able to close itself, so I pass the setter function down to the child and call it from there which updates the state in the parent component.
@Jboncz You can also just hoist the state and pass the setters and getters.
Australian Cattle DogOP
Thanks a lot! I think this is what I will go with
@Australian Cattle Dog Thanks a lot! I think this is what I will go with
Nice, good idea I think 😄 Go ahead and resolve this post using the instructions sent by @Oriental bot in the second message of the thread.