1

试图以合理的方式处理数据。我有这个 graphql 查询返回数据并记录它。问题是如何访问以下组件中的数据。

let component = ReasonReact.statelessComponent("Home");

let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      <FetchEpisodes>
        (
          (response) => {
            Js.log(response);
            /* let episodeItems =
               Array.of_list(
                 List.map(
                   (response) =>
                     <Episode
                       key=response##data##allEpisodes##id
                       episode=response##data##allEpisodes##episode
                     />,
                 )
               ); */
            <div> <h1> (ReasonReact.stringToElement("Episodes!")) </h1> </div>
            /* (ReasonReact.arrayToElement(episodeItems)) */
          }
        )
      </FetchEpisodes>
    </View>
};

这是查询响应: 在此处输入图像描述

来自 JS 我一直想用一些类似 response.data 的方式记录 allEpisodes ......这显然在这里不起作用

组件的要点:剧集组件home.re 组件

如果我取消注释并运行,它会产生以下错误:

```

FAILED: src/pages/home.mlast
/usr/local/lib/node_modules/bs-platform/bin/bsc.exe -pp "/usr/local/lib/node_modules/bs-platform/bin/refmt3.exe --print binary" -ppx '/usr/local/lib/node_modules/bs-platform/bin/reactjs_jsx_ppx_2.exe'   -w -30-40+6+7+27+32..39+44+45+101 -nostdlib -I '/Users/shingdev/code/REASON/with-reason-apollo-master/node_modules/bs-platform/lib/ocaml' -bs-super-errors -no-alias-deps -color always -c -o src/pages/home.mlast -bs-syntax-only -bs-binary-ast -impl /Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re
File "/Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re", line 53, characters 17-18:
Error: 2806: <UNKNOWN SYNTAX ERROR>

  We've found a bug for you!
  /Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re

  There's been an error running Reason's refmt parser on a file.
  This was the command:

  /usr/local/lib/node_modules/bs-platform/bin/refmt3.exe --print binary '/Users/shingdev/code/REASON/with-reason-apollo-master/src/pages/home.re' > /var/folders/qx/xhwh5zfj7z36_bjh5187svx00000gn/T/ocamlpp530e68

```

我不明白在返回数组时如何处理响应对象。谢谢你。

根据@glennsl 的建议更新:

```
let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      /* <Greeting name="Tony" /> */
      <FetchEpisodes>
        (
          (response) => {
            let episodeItems =
              response##data##allEpisodes
              |> Array.map((episode) => <Episode key=episode##id title=episode##title />);
            <div> <h1> (ReasonReact.stringToElement("Episodes!")) </h1> </div>(
              ReasonReact.arrayToElement(episodeItems)
            )
          }
        )
      </FetchEpisodes>
    </View>
};
```

这会产生以下错误:

在此处输入图像描述

我正在考虑它的到来,因为类型没有被传递给 episode.re

```
let component = ReasonReact.statelessComponent("Episode");

let make = (~style=?, ~episode, _children) => {
  ...component,
  render: (_self) => <View ?style> <h1> (ReasonReact.stringToElement(episode)) </h1> </View>
};
```

我应该在某处传递列表(剧集)吗?

更新 2:感谢@glennsl,这段代码可以在 JSX 上运行

```

let make = (_) => {
  ...component,
  render: (_self) =>
    <View>
      <Hello message="Hello from home component" />
      /* <Greeting name="Tony" /> */
      <FetchEpisodes>
        (
          (response) => {
            let episodeItems =
              response##data##allEpisodes
              |> Array.map((episode) => <Episode key=episode##id episode=episode##episode />);
            <div>
              <h1> (ReasonReact.stringToElement("Episodes!")) </h1>
              (ReasonReact.arrayToElement(episodeItems))
            </div>
          }
        )
      </FetchEpisodes>
    </View>
};
```
4

2 回答 2

1

这应该有效,我认为:

type episode = {. "id": string, "title": string, "episode": string};
type data = {. "allEpisodes": array(episode)};

...

(response) => {
  let episodeItems =
    response##data##allEpisodes
    |> Array.map((episode) =>
       <Episode key=episode##id
                episode=episode##episode />);

  <div>
    <h1> (ReasonReact.stringToElement("Episodes!")) </h1>
    (ReasonReact.arrayToElement(episodeItems))
  </div>
}

如果没有,请告诉我,或者如果这让您感到困惑,我会很乐意解释。

编辑:您还输入data##allEpisodeslist(episode)实际的array(episode). 更新了上面的代码块。Alist与 an 不同array,前者是链表类型,后者相当于 JavaScript 数组。

编辑 2:固定 JSX

于 2017-11-10T15:26:40.077 回答
0
response##data##allEpisodes
|> Array.map((episode) =>

这不起作用,因为response##data##allEpisodes返回未定义。我相信这是因为在 [reason-apollo]:https data ://github.com/Gregoirevda/reason-apollo/blob/master/src/ReasonApollo.re#L29被输入为字符串。

于 2017-11-10T18:52:29.343 回答