Benjamin Li
Benjamin Li

Reputation: 1697

state in react with redux

I have 3 questions about state in react with redux

the first one is that, as I know, state is an object. But from where can I know the fields in the state object?

the second one is that, from the todomvc official example, there is one state:

state = {
    editing: false
  }

  handleDoubleClick = () => {
    this.setState({ editing: true })
  }

in my understanding, setState would update the global state. so does that mean this state is global not local? and the state only has one field editing?

 state = {
        editing: false
      }

my last question is that, currently I am using angular to develop my website, which has 3 pages, e.g. www.example.com/invoice, www.example.com/customers, and www.example.com/suppliers. for each page, I would use angular to fetch data from backend. so if I want to rewrite it in react with redux, the state should be something like below?

{
    type: xxx,
    invoiceData: xxx,
    customerData: xxx,
    supplierData: xxx,
    //some other cool stuff
}

Upvotes: 0

Views: 86

Answers (1)

César Landesa
César Landesa

Reputation: 2656

  1. Usually, if you want to change or access the state, you have previously defined its fields. That's what you do with state = {editing: false}.

  2. I think that you are mixing react's state with Redux. The state of Redux is global to the app, but in this example you are not using Redux. This state is React's component state, so it's only accessible locally in the component. setState is a React function that asynchronously mutates the state of the component. After setState is called, the component is re-rendered.

  3. I think that this question is unclear because, as I said, you might be confused with React and Redux. Try to understand React by itself first and then take a look at Redux.

Upvotes: 1

Related Questions