N
N
Nyppy2019-10-30 16:35:56
React
Nyppy, 2019-10-30 16:35:56

How to add a new value to state in React?

I want to use state to transfer data between two components and I want the parent component to add new values ​​\u200b\u200bin the state at the right time, thereby the child component updates the table when the data in the state changes. How is this possible to do??
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Suntsev, 2019-10-31
@Nyppy

If you need to dynamically add a new value, then

  1. create an array (for example state = {array : []})
  2. in the parent component, push data into it
    const newArray = this.state.array
    newArray.push('test')
    this.setState({array : newArray})

  3. There is more data in total, and there is no need to dynamically create new variables in the state, we must understand what state is there initially

D
davidnum95, 2019-10-30
@davidnum95

https://ru.reactjs.org/docs/lifting-state-up.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question