Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 只是在父组件中进行查询,而不呈现任何内容,并将所有数据作为道具发送_Reactjs_Graphql_Apollo - Fatal编程技术网

Reactjs 只是在父组件中进行查询,而不呈现任何内容,并将所有数据作为道具发送

Reactjs 只是在父组件中进行查询,而不呈现任何内容,并将所有数据作为道具发送,reactjs,graphql,apollo,Reactjs,Graphql,Apollo,我希望在父组件中进行一个查询,而不呈现任何内容,并将数据传递到某个子组件。 我们如何在react和graphql中实现这一点 { ({loading,error,data}) =>{ if(loading) return <h4>Loading...</h4> if(error) console.log(erro

我希望在父组件中进行一个查询,而不呈现任何内容,并将数据传递到某个子组件。 我们如何在react和graphql中实现这一点

               {
                   ({loading,error,data}) =>{
                     if(loading) return <h4>Loading...</h4>
                       if(error) console.log(error)
                       console.log("graphql data is ",data);

                       return(
                       <div className="body">
                                  </div>


                               </div>


                       );
                 }
               }

              </Query>

{
({加载,错误,数据})=>{
如果(加载)返回加载。。。
if(错误)console.log(错误)
log(“graphql数据是”,数据);
返回(
);
}
}
我想在父组件中进行此查询,并将其作为子组件传递。 对于简单的api,我可以简单地调用api并将其存储在我的redux中,然后将这些数据作为道具保存在child中。
在不渲染任何内容的情况下,如何实现同样的效果?

您可以创建一个组件来渲染其子组件。下面是一个如何在react中执行此操作的示例

像这样的

class Query extends Component {
  state = {
    loading: true,
    error: {},
    data: {}
  }
  componentDidMount() {
    //do the apicall here
    this.setState({ loading: false, data: { header: "may the force be with you" } });
  }

  render() {
    const { state: props, props: { children } } = this;

    // return the data here
    // passing state as props here after renaming state to props
    return children(props);
  }
}
    <Query>
      {
        ({ loading, error, data }) => {
          if (loading) return <>Loading.......</>
          if (Object.keys(error).length > 0) return <>Some Error Handler</>
          if (data) return (
            <>
              <img src={logo} className="App-logo" alt="logo" />
              <a
                className="App-link"
                href="https://reactjs.org"
                target="_blank"
                rel="noopener noreferrer"
              >
                {data.header}
              </a>
            </>
          )
        }
      }
    </Query>
使用react状态而不是redux。如果要使用redux,可以创建此已连接组件。然后使用这样的组件

class Query extends Component {
  state = {
    loading: true,
    error: {},
    data: {}
  }
  componentDidMount() {
    //do the apicall here
    this.setState({ loading: false, data: { header: "may the force be with you" } });
  }

  render() {
    const { state: props, props: { children } } = this;

    // return the data here
    // passing state as props here after renaming state to props
    return children(props);
  }
}
    <Query>
      {
        ({ loading, error, data }) => {
          if (loading) return <>Loading.......</>
          if (Object.keys(error).length > 0) return <>Some Error Handler</>
          if (data) return (
            <>
              <img src={logo} className="App-logo" alt="logo" />
              <a
                className="App-link"
                href="https://reactjs.org"
                target="_blank"
                rel="noopener noreferrer"
              >
                {data.header}
              </a>
            </>
          )
        }
      }
    </Query>

{
({加载,错误,数据})=>{
如果(装载)返回装载。。。。。。。
if(Object.keys(error).length>0)返回一些错误处理程序
如果(数据)返回(
)
}
}

您可以创建一个组件来渲染其子组件。下面是一个如何在react中执行此操作的示例

像这样的

class Query extends Component {
  state = {
    loading: true,
    error: {},
    data: {}
  }
  componentDidMount() {
    //do the apicall here
    this.setState({ loading: false, data: { header: "may the force be with you" } });
  }

  render() {
    const { state: props, props: { children } } = this;

    // return the data here
    // passing state as props here after renaming state to props
    return children(props);
  }
}
    <Query>
      {
        ({ loading, error, data }) => {
          if (loading) return <>Loading.......</>
          if (Object.keys(error).length > 0) return <>Some Error Handler</>
          if (data) return (
            <>
              <img src={logo} className="App-logo" alt="logo" />
              <a
                className="App-link"
                href="https://reactjs.org"
                target="_blank"
                rel="noopener noreferrer"
              >
                {data.header}
              </a>
            </>
          )
        }
      }
    </Query>
使用react状态而不是redux。如果要使用redux,可以创建此已连接组件。然后使用这样的组件

class Query extends Component {
  state = {
    loading: true,
    error: {},
    data: {}
  }
  componentDidMount() {
    //do the apicall here
    this.setState({ loading: false, data: { header: "may the force be with you" } });
  }

  render() {
    const { state: props, props: { children } } = this;

    // return the data here
    // passing state as props here after renaming state to props
    return children(props);
  }
}
    <Query>
      {
        ({ loading, error, data }) => {
          if (loading) return <>Loading.......</>
          if (Object.keys(error).length > 0) return <>Some Error Handler</>
          if (data) return (
            <>
              <img src={logo} className="App-logo" alt="logo" />
              <a
                className="App-link"
                href="https://reactjs.org"
                target="_blank"
                rel="noopener noreferrer"
              >
                {data.header}
              </a>
            </>
          )
        }
      }
    </Query>

{
({加载,错误,数据})=>{
如果(装载)返回装载。。。。。。。
if(Object.keys(error).length>0)返回一些错误处理程序
如果(数据)返回(
)
}
}

虽然在
查询
组件之外有很多方法可以获取数据,但不清楚为什么需要这样做。如果数据仅由子组件而不是父组件需要,则使用查询组件包装子组件。即使你有多个孩子,您可以安全地为每个查询组件重复使用相同的查询组件,而不会触发多个网络请求。您是否找到了解决方法,或者您是否仍在寻找解决方案。不,我无法。由于对apollo和apollo中的状态管理知识有限,仍在寻找任何提示。我想我在react中对此有一个解决方法。我已经把答案贴在下面了。让我知道是否适合您。虽然在
Query
组件之外有很多方法可以获取数据,但不清楚您为什么需要这样做。如果数据仅由子组件而不是父组件需要,则使用查询组件包装子组件。即使你有多个孩子,您可以安全地为每个查询组件重复使用相同的查询组件,而不会触发多个网络请求。您是否找到了解决方法,或者您是否仍在寻找解决方案。不,我无法。由于对apollo和apollo中的状态管理知识有限,仍在寻找任何提示。我想我在react中对此有一个解决方法。我已经把答案贴在下面了。让我知道是否适合你。