D
D
Daniel2020-01-01 00:24:48
React
Daniel, 2020-01-01 00:24:48

How to restructure props data and use it in state?

I'll show you with an example.

A props.bartwo-dimensional array was passed to . To use it in a component, each subarray needs to be "flipped" ( ) I planned to solve this problem as follows:[ [1], [1, 2], [1, 2, 3] ]
state.reverse()



class Foo extends Component {
  constructor(props) {
    super(props)

    this.state = {
      bar: this.restruct( props.bar )
    }
  }

  render() {
    console.log(this.state.bar)

    return( <div></div> )
  }

  restruct(data) {
    return data.map( arr => arr.reverse() )
  }

}


But this method does not work - subarrays do not change; does not give any errors or warnings. No matter how I dodge: I created a variable before assigning state and many more ... - the same

result

Thanks to those who responded.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniel, 2021-01-04
@TheCrossCarrier

Until I wrote it reversemanually, nothing worked, I don’t know why this happened.
If you know - write ideas.

P
Pavel Shvedov, 2020-01-01
@mmmaaak

https://stackblitz.com/edit/react-fshgns?file=src/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question