jbr*_*own 10 javascript reactjs relayjs
我有一个createObject突变,返回新对象的ID.
返回后,我想重定向到关于新对象的详细信息页面.
如何使用react/relay从包含组件中的变异中获取响应字段?
例如,我的createObject页面包含突变,代码如下:
var onFailure = (transaction) => {
};
var onSuccess = () => {
redirectTo('/thing/${newthing.id}'); // how can I get this ID?
};
// To perform a mutation, pass an instance of one to `Relay.Store.update`
Relay.Store.update(new AddThingMutation({
userId: this.props.userId,
title: this.refs.title.value,
}), { onFailure, onSuccess });
}
Run Code Online (Sandbox Code Playgroud)
newthing 应该是变异创建的对象,但我怎样才能掌握它?
ste*_*her 19
通常我们会配置突变的客户端RANGE_ADD并thingEdge从突发的服务器端返回一个新的,但是在这里你没有在客户端上添加新节点的范围.要告诉Relay获取任意字段,请使用REQUIRED_CHILDRENconfig.
var AddThingMutation = mutationWithClientMutationId({
/* ... */
outputFields: {
newThingId: {
type: GraphQLID,
// First argument: post-mutation 'payload'
resolve: ({thing}) => thing.id,
},
},
mutateAndGetPayload: ({userId, title}) => {
var thing = createThing(userId, title);
// Return the 'payload' here
return {thing};
},
/* ... */
});
Run Code Online (Sandbox Code Playgroud)
class AddThingMutation extends Relay.Mutation {
/* ... */
getConfigs() {
return [{
type: 'REQUIRED_CHILDREN',
// Forces these fragments to be included in the query
children: [Relay.QL`
fragment on AddThingPayload {
newThingId
}
`],
}];
}
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
var onFailure = (transaction) => {
// ...
};
var onSuccess = (response) => {
var {newThingId} = response.addThing;
redirectTo(`/thing/${newThingId}`);
};
Relay.Store.update(
new AddThingMutation({
title: this.refs.title.value,
userId: this.props.userId,
}),
{onSuccess, onFailure}
);
Run Code Online (Sandbox Code Playgroud)
请注意,使用此技术查询的任何字段都将可用于onSuccess回调,但不会添加到客户端存储中.
| 归档时间: |
|
| 查看次数: |
1794 次 |
| 最近记录: |