简体   繁体   English

使用中继环境的突变

[英]Mutations using Relay Environment

I'm using Relay with React Native and have a problem during login & logout. 我正在使用Relay与React Native并在登录和注销期间遇到问题。

After login or logout, Relay keeps the store from the previous user. 登录或注销后,Relay会将商店与之前的用户保持一致。 To solve this I use Relay.Renderer and Relay.Environment . 为了解决这个问题,我使用了Relay.RendererRelay.Environment As in, in each Renderer I put singleton object of Environment . 就像在每个Renderer我放置Environment单个对象。

The problem is that I previously did a mutation on object of Relay.Store , as in Relay.Store.commitUpdate(new CreateProfile(), callback) . 问题是,我以前做了一个突变的对象Relay.Store ,如Relay.Store.commitUpdate(new CreateProfile(), callback)

Now it doesn't work. 现在它不起作用。 I guess this is because Relay.Store doesn't know anything about server endpoints. 我想这是因为Relay.Store对服务器端点一无所知。 But Relay.Environment does. 但是Relay.Environment确实如此。

And now I'm using something like this this.props.relay.commitUpdate(new CreateProfile(), callback) . 现在我正在使用这样的东西this.props.relay.commitUpdate(new CreateProfile(), callback) It works pretty well when the parent component is wrapped as Relay.Container , so it has relay object in props. 当父组件被包装为Relay.Container ,它工作得很好,所以它在props中有中继对象。

But what should I do in components which are not Relay.Containers and don't have Relay object in props? 但是,我应该在不是Relay.Containers组件中做什么,并且在道具中没有Relay对象?

Relay.Store is a globally accessible singleton instance of Relay.Environment and Relay.Store.commitUpdate() updates data in that global environment. Relay.Store是全球可访问的单一实例Relay.EnvironmentRelay.Store.commitUpdate()在全球环境的更新数据。 But since you're using your own instance of Relay.Environment , to update it you need to use this.props.relay.commitUpdate() , as you noted. 但是,由于您正在使用自己的Relay.Environment实例,要更新它,您需要使用this.props.relay.commitUpdate() ,如您所述。 This updates the environment the container was rendered with. 这会更新容器呈现的环境。

If need to make mutations from child components of containers , that are not wrapped in a Relay.Container , there are two ways to do that. 如果需要从容器的子组件进行突变 ,而这些组件未包含在Relay.Container ,则有两种方法可以做到这一点。 You could simply pass the relay prop to them, so in the render function of your container you would have: 您可以简单地将relay道具传递给它们,因此在容器的渲染功能中,您将拥有:

<Child relay={this.props.relay} />

However, since those plain components are not in a Relay container, they don't currently need to know anything about Relay. 但是,由于这些普通组件不在Relay容器中,因此它们目前不需要了解有关Relay的任何信息。 If you want to keep them that way, you could write the method that does the update in your container component like this: 如果你想保持这种方式,你可以编写在容器组件中执行更新的方法,如下所示:

onCreateProfile = () => {
  this.props.relay.commitUpdate(new CreateProfile());
};

and only pass a callback to your child component in render : 并且只在render中将回调传递给子组件:

<Child onCreateProfile={this.onCreateProfile} />

If you need to make a mutation from a component that does not have a Relay.Container above it in the component hierarchy at all, you could create the Relay.Environment in a shared root component higher up and pass it down using props (or pass a callback using the strategy shown above). 如果你需要从组件层次结构中没有Relay.Container的组件中进行变异 ,你可以在更高的共享根组件中创建Relay.Environment并使用props传递它(或传递使用上面显示的策略进行回调。

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

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