Define a variabel state

Define a variabel state
0

#1

I am writing the state in React.

How can i define a variable in the state, which refers to another state.

I tried some variations, but nothing runs.

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state={
      breakLength: "5",
      sessionLength: "25",
      timeLeftMinutes: {sessionLength},
      timeLeftSeconds: "0"
    }
  }

#2

Any reason why you are doing this? They are going to be duplicates.


#3

Any reasons for not just assigning it 25? This is just the initial state, there is no need to get to fancy about it.


#4

First off, you shouldn’t be storing numbers as strings unless they are for presentation only. Usually, you want to store numbers as numbers, then convert or coerce to a string only at the point you display them in the UI. Storing numbers as strings can lead to some weird bugs, especially if you coerce them implicitly (for example: Number(Boolean('0')) === 1).

To answer your question, though, you can define the variable before you use it within the constructor function, then set both items to the variable. Trying to set a property to a value of another property of the same object at the same time as setting that other property on the object will never work.