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

  • 0
    Hello everyone, I study react, such a question - I make a request to the server, get users and organizations for further output through the components, I organized the code in this way. I wanted to know how much is correct? In the future, there will be a filter of users by the organization to which it belongs, it turns out the state will change and I think how to organize everything 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 }));
      }
    }
    JavaScript Anonymous, Nov 9, 2019

  • 1 Answers
  • 0
    Yes. Everything that affects the rendering of a component and changes its value during the life cycle must be inside the state
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!