8

I know that state allows us to create components that are dynamic and interactive but I want to deep in state.

Can someone help me to understand state in React using a real life example?

grooveplex
  • 2,492
  • 4
  • 28
  • 30
Jon
  • 461
  • 2
  • 7
  • 17
  • 2
    The state of a component is the state of a component? The name says it all. – Jonas Wilms Jan 26 '19 at 15:43
  • Is there anything *in particular* you have trouble understanding about state? Review [ask]. :) – grooveplex Jan 26 '19 at 15:45
  • 3
    State in real life example: Before some upvoted your question, you can imagine your question or think it as question component had `vote state = 0` and after that it became 1 and so on. So interactivity with the application changed something in the application. That changed something can be called state. – Sahil Raj Thapa Jan 26 '19 at 15:49
  • 2
    A good place to start is the documentation. https://reactjs.org/docs/faq-state.html and https://reactjs.org/docs/state-and-lifecycle.html – Titus Jan 26 '19 at 15:55
  • 1
    Possible duplicate of [What is the difference between state and props in React?](https://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react) – Alwaysblue Jan 26 '19 at 16:26
  • @JonasWilms, Hi Jonas, is "state" essentially used in terms of user interaction. Where else state can be used. Or can I imagine that State is only used on user interaction? – Dickens Sep 10 '19 at 15:23
  • @dickens no. It makes sense to describe every asynchronous data as state, e.g. data coming from the server, the user, other threads, whatever. – Jonas Wilms Sep 10 '19 at 19:59

6 Answers6

7
import React from 'react';

class App extends React.Component {
  state = {
    count: 0
  };

  render() {
    return (
      <div>
        <h1>Hello world</h1>
        <h2>Count: {this.state.count}</h2>
        <button
          onClick={() => this.setState(state => ({ count: state.count + 1 }))}
        >
          +
        </button>
        <button
          onClick={() => this.setState(state => ({ count: state.count - 1 }))}
        >
          -
        </button>
      </div>
    );
  }
}

export default App;

In the above code, it has a state object with property/state: count.

State can simply be understand as a value at that point of time of the particular component/app. In the above example, when the app is first running, the app is with state count === 0

As we can see there are two buttons + and - that update the value using this.setState, it simply update the "state" of count for the app, and the app will re-render, whenever the state change

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • hi, is "state" essentially used in terms of user interaction. Where else state can be used. Or can I imagine that State is only used on user interaction? – Dickens Sep 10 '19 at 15:20
  • @Dickens: You should imagine `state` as `variable` and it's `value` at that particular point of time. – Isaac Sep 11 '19 at 01:25
4

For example:

import React, { Component } from "react";
import ReactDOM from "react-dom";

class App extends Component {
  state = {
    show: false,
  }

  showTextToggle = () => {
    this.setState({ show: !this.state.show });
  }

  render() {
    const { show } = this.state;
    return (
      <div>
        <h3>Some title</h3>
        {show ? <div>Description</div> : undefined}
        <button onClick={this.showTextToggle}>Read more</button>
      </div>
    )
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

P.S. CodeSandBox

meine
  • 260
  • 5
  • 15
4

State in real life example:

Before someone upvoted your question, you can imagine your question or think it as question component had vote state = 0 and after that it became 1 and so on. So interactivity with the application changed something in the application. That changed something/ value can be called state.

State in application/ component can change due to interactivity(event) or during time.

As time you can imagine this Post or Post Component before 30 minutes/ some time ago had no answer i.e answer state = 0. And now it has some (3) answers. So answer state = 0 changed to answer state = 3.

State is just a value that a component/app is in at particular time.

Just imagine the specific point of time when you posted this question and now see the changes in this post. This changes can be thought as change in the state of the component/ app.

Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23
  • hi, is "state" essentially used in terms of user interaction. Where else state can be used. Or can I imagine that State is only used on user interaction? – Dickens Sep 10 '19 at 15:20
  • No, state can be used to show/ indicate particular condition of your app in a specific time. eg to show loader while requesting data you can use `loader: true` as state and before and after requesting data you can use `loader: false` as state. So state can change frequently and events (eg: user interaction) changes them. – Sahil Raj Thapa Sep 11 '19 at 03:32
1

The state is an instance of React Component Class can be defined as an object of a set of observable properties that control the behaviour of the component. In other words, the State of a component is an object that holds some information that may change over the lifetime of the component. For example, let us think of the clock that we created in this article, we were calling the render() method every second explicitly, but React provides a better way to achieve the same result and that is by using State, storing the value of time as a member of the component’s state. We will look into this more elaborately later in the article.

Abdulbaqi
  • 11
  • 3
0

State allows you to create components that are dynamic and interactive.

It holds information about the current state of the app. You can keep your variables and your data inside the local state of the React app by doing,

class App extends Component{
    state = {
        didUserLogin: false
    }
}

Then, you can directly access the state using,

    console.log(this.state.didUserLogin);

or update it with,

    this.setState({didUserLogin: true});

As you keep manipulating the data inside the state, the components that use the state will be updated with the information you provided for the state.

More information,

Fatih Aktaş
  • 1,446
  • 13
  • 25
0

class App extends Component {
    constructor(props){
        super(props);
        this.state = {products: []};
    }

    render() {
        console.log(this.state);
        return (
            <div className="container">
            </div>
        );
    }
}
In the latest react, we now can define state as a plain JavaScript object and the render method gets it correctly. Here is an example of state as a plain object outside constructor:

In the latest react, we now can define state as a plain JavaScript object and the render method gets it correctly. Here is an example of state as a plain object outside constructor:

class App extends Component {
    constructor(props){
        super(props);
    }
    state = {products: []}
    
    render() {
        console.log(this.state);
        return (
            <div className="container">
            </div>
        );
    }
}

More Details https://www.codegreet.com/how-to-use-state-in-reactjs/

Kalpesh K
  • 1
  • 1