Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.state vs state in React

I'm working in a new codebase. Normally, I would set up state like this in a React component:

class App extends React.Component {
    constructor() {
        super();
        this.state={
            foo: 'bar'
        }
     }
    ....

In this new codebase, I'm seeing a lot of this:

class App extends React.Component {
    state={
        foo: 'bar'
     }
    ....

Is there an advantage to doing it this way? They seem to only do it when state doesn't need to be altered. I always thought of state as being something React handled. Is this an ok thing to do?

like image 683
J Seabolt Avatar asked Jun 30 '18 20:06

J Seabolt


People also ask

What does this state do in React?

What Is 'State' in ReactJS? The state is a built-in React object that is used to contain data or information about the component. A component's state can change over time; whenever it changes, the component re-renders.

Why we use this state in react JS?

React uses an observable object as the state that observes what changes are made to the state and helps the component behave accordingly. For example, if we update the state of any component like the following the webpage will not re-render itself because React State will not be able to detect the changes made.

What is the difference between this state and this setState?

state as if it were immutable. setState() does not immediately mutate this. state but creates a pending state transition. Accessing this.


2 Answers

The end result of both approaches is the same. Both approaches are just setting the initial state of the component. It's worth noting that class properties are a stage 3 proposal, so all development environments may not be able to use them.

I personally like to use the class field variant if nothing else is done in the constructor, as it is less code to write, and you have no super call to worry about.

Example

class Component1 extends React.Component {
  state = { value: this.props.initialValue };

  render() {
    return <div> {this.state.value} </div>
  }
}

class Component2 extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: props.initialValue };
  }

  render() {
    return <div> {this.state.value} </div>
  }
}

function App() {
  return (
    <div>
      <Component1 initialValue={1} />
      <Component2 initialValue={2} />
    </div>
  );
}
like image 147
Tholle Avatar answered Oct 21 '22 15:10

Tholle


Actually both of them bind to this pointer. the this that made in constructor of class.

Totally you can access to local state by this.state but in first style you can pass props to constructor by super and then use it in state declaration, just like below:

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            foo: 'bar',
            jaz: props.someParentState,
        }
     }
....

Awesome, you can access to props in constructor, isn't pretty? I definitely use this style for local state declaration.

Hope this helps you.

like image 25
AmerllicA Avatar answered Oct 21 '22 14:10

AmerllicA