Skip to content
Advertisement

Output data from form on the page react

I am writing todo app. There are main files in my directory now:

App (rendering main page with header and buttons)

JavaScript

TodoForm (create a form)

JavaScript

TodoBox (generating list of tasks)

JavaScript

And the question is: how I can pass the state from TodoForm to TodoBox in App (it is initialize as an empty array now). I want to output tasks at the bottom of the same page in container after header element.

Advertisement

Answer

You can create a function (addTodo) in App component and pass it down to the TodoForm component. In TodoForm component you can invoke the addTodo function from props and send the todoValue as arguments props.addTodo(todoValue). In addTodo function in App component you can update the todoValue to state. Once you update the state it will re-render the App component, then the TodoBox component will call with the updated todoValue value.

Note: But it is not best practice. The best practice is to use React Context

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement