A
A
Alexandra2020-11-18 14:56:19
JavaScript
Alexandra, 2020-11-18 14:56:19

How to correctly display the response from the react server in state?

Hello everyone, I'm studying react, such a question - I make a request to the server, I get users and organizations for further output through the components, I organized the code in this way. I wanted to know if this is correct? In the future, there will be a user filter by the organization to which it belongs, it turns out the state will change and I think how to organize it all correctly, probably the array with users should be inside the state

class App extends Component {
  state = {
    loading: true,
    selectedOrg: null
  };
  users = [];
  organizations = [];
  componentDidMount() {
    getUsers()
      .then((users) => (this.users = users))
      .then(() => getOrganizations())
      .then((organizations) => (this.organizations = organizations))
      .then(() => this.setState({ loading: false }));
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-11-18
@AlexaShemetova

Yes. Everything that affects the rendering of the component and changes the value during the life cycle should be inside the state

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question