V
V
volucris12021-04-05 14:08:34
React
volucris1, 2021-04-05 14:08:34

How to copy a variable in React in componentDidMount?

componentDidMount() {
        this.setState({
            x: 1
            y: 2
            z: x-y 
        })
    }

This will throw an error (z will be NaN), how can I avoid it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-04-05
@volucris1

Traditionally, to a poorly formulated question, a deliberately useless answer is:

componentDidMount() {
  this.setState({
    x: 1
    y: 2
    z: 1-2 
  })
}

Describe the problem properly. What for in general to store in a state that it is possible to calculate easily? And if you still need to store, then what prevents you from calculating not from some non-existent variables?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question