【问题标题】:How to return Highcharts in Apollo Query component?如何在 Apollo Query 组件中返回 Highcharts?
【发布时间】:2018-10-09 21:06:41
【问题描述】:

我正在尝试在 Highchart 中返回 graphql 数据。不知何故,它没有被显示。观看现场演示here

import React from "react";
import { Query } from "react-apollo";
import Highcharts from "highcharts";

import { ExchangeRates, client } from "./query";

class OtherComponentTest extends React.Component {
  componentDidMount() {
    <Query query={ExchangeRates} client={client}>
      {({ loading, error, data }) => {
        if (error) return <h1>Error...</h1>;
        if (loading || !data) return <h1>Loading...</h1>;

        return Highcharts.chart("production", {
          ...
        });
      }}
    </Query>;
  }
  render() {
    return (
      <div>
        <div id="production">loading </div>
      </div>
    );
  }
}

export default OtherComponentTest;

我错过了什么?

【问题讨论】:

    标签: reactjs highcharts apollo


    【解决方案1】:

    您不能像这样将 JSX 添加到您的 componentDidMount 方法中并期望它做某事。您的代码只会被转译为对React.createElement 的调用,但元素本身将永远不会被渲染。有几种不同的方法可以做到这一点:

    一,为你的 highcharts 容器创建一个包装器组件。

    // elsewhere...
    class HighchartsComponent extends React.Component {
      componentDidMount () {
        Highcharts.chart("production", {
          series: this.props.series,
          // other config
        })
      }
    
      render () {
        return <div id="production">loading</div>
      }
    }
    
    // inside whatever render method
    <Query query={ExchangeRates} client={client}>
      {({ loading, error, data }) => {
        if (error) return <h1>Error...</h1>;
        if (loading || !data) return <h1>Loading...</h1>;
    
        return <HighchartsComponent series={data.someQuery.someField}/>
      }}
    </Query>
    

    两个,使用ApolloConsumer 组件或withApollo HOC 直接访问您的 Apollo 客户端实例。在componentDidMount 内部,您可以直接调用query 方法并使用结果。比如:

    const { data } = await this.props.client.query({ query: ExchangeRates })
    if (data && data.someQuery) {
      Highcharts.chart("production", {
        series: data.someQuery.someField,
        // other config
      })
    }
    

    三,使用 official library 来自 Highcharts 团队的 React。

    import Highcharts from 'highcharts'
    import HighchartsReact from 'highcharts-react-official'
    
    <Query query={ExchangeRates} client={client}>
      {({ loading, error, data }) => {
        if (error) return <h1>Error...</h1>;
        if (loading || !data) return <h1>Loading...</h1>;
    
        return (
          <HighchartsReact
            // your props here
          />
        )
      }}
    </Query>
    

    【讨论】:

    • 好的,我明白了。我的直播demo 有什么问题?以及如何在 options 常量中定义数据?
    • @Peterhack 您的问题不包含演示链接。
    • 是的,我期望的 CodeSandbox 出了点问题。即使您将 highcharts-react-official 添加到依赖项中,它似乎也找不到它。还有其他使用相同库的示例似乎可以正常工作(codesandbox.io/s/50p5l109nn
    • 谢谢!我更新了我的demo。关于highcharts react,你只需在codesandbox中删除它并再次添加它,然后它就可以工作了。唯一不知道怎么解决的是const options下的数据字段如何引用查询到的data.rates
    • 您可以在Query 组件的渲染函数中移动options。那么它将与数据在同一范围内
    猜你喜欢
    • 2018-09-19
    • 2019-10-05
    • 2019-01-15
    • 2018-10-30
    • 1970-01-01
    • 2019-09-23
    • 2021-06-09
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多