How to setState() in React/Apollo with graphQL

前端 未结 3 1757
醉话见心
醉话见心 2021-02-04 20:14

I am trying to setState() to a query result I have from graphQL, but I am having difficulty finding out how to do this because it will always be loading, or it\'s only used from

相关标签:
3条回答
  • 2021-02-04 20:41

    What is the reason behind setting it to state? Keep in mind, Apollo Client uses an internal redux store to manage queries. If you're trying to trigger a re render based on when something changes in the query, you should be using refetchQueries(). If you absolutely need to store it in local state, I would assume you could probably compare nextProps in componentWillReceiveProps to detect when loading (the value that comes back when you execute a query from apollo client) has changed, then update your state.

    0 讨论(0)
  • 2021-02-04 20:42

    I had a similar issue (although it was happening for a totally different reason). My state kept getting set to undefined. I was able to solve it with a React middleware. It made it easy to avoid this issue. I ended up using superagent.

    http://www.sohamkamani.com/blog/2016/06/05/redux-apis/

    0 讨论(0)
  • 2021-02-04 20:52

    A simple solution is to separate your Apollo query components and React stateful components. Coming from Redux, it's not unusual to transform incoming props for local component state using mapStateToProps and componentWillReceiveProps. However, this pattern gets messy with Apollo's <Query />.

    So simply create a separate component which fetches data:

    ...
    
    export class WidgetsContainer extends Component {
      render (
        <Query query={GET_WIDGETS}>
          {({ loading, error, data }) => {
            if (loading) return <Loader active inline="centered" />;
    
            const { widgets } = data;
            return (
              <Widgets widgets={widgets} />
            )
          }}
        </Query>
      )
    }
    

    And now the Widgets components can now use setState as normal:

    ...
    export class Widgets extends Component {
      ...
      constructor(props) {
        super()
    
        const { widgets } = props;
        this.state = {
          filteredWidgets: widgets
        };
      }
    
      filterWidget = e => {
        // some filtering logic
        this.setState({ filteredWidgets });
      }
    
      render() {
        const { filteredWidgets } = this.state;
        return (
          <div>
            <input type="text" onChange={this.filterWidgets} />
            {filteredWidgets.count}
          </div>
        )
      }
    }
    
    0 讨论(0)
提交回复
热议问题