简体   繁体   English

需要在react-apollo中使用从GraphQL订阅接收的数据的setstate()

[英]Need to use setstate() of data received from GraphQL subscription in react-apollo

I am trying to setState() of the GraphQL subscription Query that I am doing through react-apollo. 我正在尝试通过react-apollo执行的GraphQL订阅查询的setState()。 My purpose is to store the complete the object that I received from GraphQL and save it into the state in the ComponentDidMount() method of the App.js file. 我的目的是存储从GraphQL接收的完整对象,并将其保存到App.js文件的ComponentDidMount()方法中的状态。

I have tried a lot of ways for it to work such as SubscribetoMore endpoint by react-apollo, but I think I am writing the wrong react code for it. 我已经尝试了许多方法使其工作,例如react-apollo的SubscribetoMore端点,但是我认为我为此编写了错误的react代码。

/App.js /App.js

const haveAnyFish = () => (
    <Subscription subscription={gql`${SUBSCRIPTION_SYNC}`} >
{({ loading, error, data }) => {
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :</p>;
  return (
    <div>
      <div>{fishes(data)}</div>
    </div>
  );
}}
</Subscription>
);

/App.js /App.js

class App extends React.Component {
    state = {
      fishes: {},
      order: {}
    };
componentDidMount() {
    const fishes = (data) => {
        this.setState({ fishes: data });
    }
  }

Subscription Query 订阅查询

const SUBSCRIPTION_SYNC = `
    subscription syncState{
      cotd {
      _id_2
      name
      desc
      image
      price
      status
    }
  }`;

You no need to have componentDidMount in your case. 您无需在您的情况下使用componentDidMount。 You have defined function inside componentDidMount method and that can't be accessed outside 您已经在componentDidMount方法内部定义了函数,并且无法在外部访问

Change 更改

   componentDidMount() {
        const fishes = (data) => {
            this.setState({ fishes: data });
        }
    }

To

  fishes = data => {
        this.setState({ fishes: data });
  }

And Change 和改变

    const haveAnyFish = () => (
        <Subscription subscription={gql`${SUBSCRIPTION_SYNC}`} >
          {({ loading, error, data }) => {
              if (loading) return <p>Loading...</p>;
              if (error) return <p>Error :</p>;
           return (
             <div>
                <div>{this.fishes(data)}</div>
                </div>
             );
           }}
         </Subscription>
        );

To

     haveAnyFish = () => (
          <Subscription subscription={gql`${SUBSCRIPTION_SYNC}`} >
         {({ loading, error, data }) => {
          if (loading) return <p>Loading...</p>;
          if (error) return <p>Error :</p>;
             return (
                <div>
                  <div>{this.fishes(data)}</div>
                </div>
              );
           }}
        </Subscription>
      );

You must be calling haveAnyFish function in your component without this so after above code change you need to call haveAnyFish function using this.haveAnyFish 您必须在没有此组件的情况下在组件中调用haveAnyFish函数,因此在上述代码更改之后,您需要使用this.haveAnyFish来调用haveAnyFish函数。

Also note that whenever you create functions inside a component they no need const before the function and if you want to access state or props inside those function you need to manually bind them or change them to arrow functions 还要注意,每当您在组件内部创建函数时,它们都不需要在该函数之前使用const;如果要访问这些函数内部的状态或道具,则需要手动绑定它们或将其更改为箭头函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM