使用Relay更新React状态 [英] Updating React state with Relay

查看:113
本文介绍了使用Relay更新React状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Relay,您可以照常创建一个React组件:

With Relay, you create a React component as usual:

class TodoApp extends React.Component {
  ...
}

然后将组件包装在Relay容器中:

And then component is wrapped in a Relay container:

export default Relay.createContainer(TodoApp, {
  ...
});

Relay容器将使用GraphQL获取数据,然后更新状态。这是一个更高阶的组件,然后这个状态作为道具传递给它的子组件。

The Relay container will fetch data using GraphQL and then update the state. This is a higher order component and this state is then passed down as props to its children.

这不是(或似乎不是)与Redux等通量实现兼容。 Redux有一个单一的全局状态对象,它也有更高阶的组件,可以将props传递给表示组件。所以我没看到Redux存储和Relay容器当前是如何共存的。

This isn't (or doesn't appear to be) compatible with a flux implementation like Redux. Redux has a single global state object and it too has higher order components that pass props down to presentational components. So I don't see how both the Redux store and Relay containers can coexist currently.

那么我们应该如何更新不是来自数据库的状态呢?这个状态应该如何通过Relay进行管理?

So then how should we update state that doesn't come from the database? How is this state supposed to be managed with Relay?

推荐答案

虽然我不能就一起使用它们提供建议, 从技术上讲,你肯定可以一个接一个地应用几个高阶组件:

While I can’t offer you an advice on using them together, technically you can definitely have several higher order components applied one after another:

class TodoApp extends React.Component {
  // ...
}

TodoApp = connect(
  // ...
)(TodoApp);

TodoApp = Relay.createContainer(TodoApp, {
  // ...
});

export default TodoApp;

我不确定这有多大意义,但它很容易实现。

I’m not sure this makes a lot of sense, but it’s easily doable.

这篇关于使用Relay更新React状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆