简体   繁体   English

使用 graphql 将道具传递给高阶组件时,React-apollo Typescript 错误

[英]React-apollo Typescript errors when using graphql to pass props to higher order component

I'm running into some issues trying to connect a React class component to my Apollo cache data, which for now is just local.我在尝试将 React class 组件连接到我的 Apollo 缓存数据时遇到了一些问题,目前这只是本地缓存数据。

I'm following the docs from here , but I'm running into trouble where VSCode and Webpack are throwing errors when I access data like this: this.props.data.playing , which is how I'd expect to access it.我正在关注这里的文档,但是当我访问这样的数据时,VSCode 和 Webpack 会抛出错误: this.props.data.playing ,这就是我希望访问它的方式。 Despite this, in the browser this returns the correct data.尽管如此,在浏览器中这会返回正确的数据。 However if I access data like this: this.props.data.data.playing the typecheck passes but throws an error in the browser console ( Cannot read property 'playing' of undefined ).但是,如果我访问这样的数据: this.props.data.data.playing检查通过但在浏览器控制台中引发错误( Cannot read property 'playing' of undefined )。 So I'm reasonably sure this is a type definition error, but I'm not sure where I'm going wrong.所以我有理由确定这是一个类型定义错误,但我不确定我哪里出错了。

I should point out that NPM and all packages are updated.我应该指出 NPM 和所有软件包都已更新。

type AudioData = {
  bpm: number;
  beatsPerBar: number;
  playing: boolean;
  metronomeSound: string;
  playPosition: number;
  playStartTime: number;
};

type Response = {
  data: AudioData;
};

class ConnectedWorkspaceAudio extends React.Component<
  ChildProps<{}, Response>
> {
  _context: AudioContext;
  _scheduler: Scheduler;
  _recorder: Recorder;

  constructor(props: ChildProps<{}, Response>) {
    super(props);
  }

  componentDidMount(): void {
    /***/
  }

  componentDidUpdate(prevProps: ChildProps<{}, Response>): void {
    console.log(this.props.data.playing); // Fails type check, prints correctly in browser.
    if (this.props.data.data) {
      if (this.props.data.data.playing && !prevProps.data.data.playing) { // Passes type check, gives console error in browser.
        useApolloClient().writeData({ data: { playing: true } });
      }
      if (!this.props.data.data.playing && prevProps.data.data.playing) {
        useApolloClient().writeData({ data: { playing: false } });
      }
    }
  }

  /** There's some more methods including render() I don't believe are relevant. */
}


const WORKSPACE_AUDIO_QUERY = gql`
  query AudioState {
    bpm @client
    beatsPerBar @client
    playing @client
    metronomeSound @client
    playPosition @client
    playStartTime @client
  }
`;

const WorkspaceAudio = graphql<{}, Response>(WORKSPACE_AUDIO_QUERY)(
  ConnectedWorkspaceAudio
);

export { WorkspaceAudio };

This is not typing error - typical error for trying access to not ready data - usually in render() , when data not arrived yet.这不是输入错误 - 尝试访问未准备好的数据的典型错误 - 通常在render()中,当数据尚未到达时。

Place console log after if (this.props.data.data) { ... and bad logging argument, should be console.log(this.props.data.data.playing);console log if (this.props.data.data) { ... 和错误的日志参数之后,应该是console.log(this.props.data.data.playing); - response named data (a bit missleading). - 响应命名data (有点误导)。

The solution here was to not wrap the AudioData type into Response , despite the fact that they use that pattern in the documentation.这里的解决方案是不将AudioData类型包装到Response中,尽管他们在文档中使用了该模式。 I updated the following lines:我更新了以下几行:

class ConnectedWorkspaceAudio extends React.Component<
  ChildProps<{}, AudioData>
{
...

const WorkspaceAudio = graphql<{}, AudioData>(WORKSPACE_AUDIO_QUERY)(
  ConnectedWorkspaceAudio
);

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

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