-1

I'm use state in this.state in constructor. how can i use immediately state after declaration?

    constructor() {
        super(...arguments);
        this.state = {
            client: null,
            client1: this.state.client,
        }
   }

2 Answers2

3

You can have your state in an object outside the assignment itself and then assigning the state to that object modifying whatever data you may want:

constructor(props) {
  super(props);
  const state = { client: null }
  this.state = { ...state, client1: state.client };
}

Check this one out: "this" inside object

corvus
  • 513
  • 1
  • 3
  • 13
0

you could do:

constructor() {
    super(...arguments);
    this.state = {
        client: null,    
    }
    this.state.client1 = this.state.client;
}

Even though, you could also assign null to client1, as @zmag mentioned, if you always assign null to this.state.client

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72