我目前正在开发一个页面,该页面显示特定用户的一组特定“任务”的状态。为了获取数据,我有以下中继容器:
export default Relay.createContainer(TaskReview, {
initialVariables: {
limit: Number.MAX_SAFE_INTEGER,
studentId: null,
},
fragments: {
task: () => Relay.QL`
fragment on Task {
id,
title,
instruction,
end_date,
taskDataList(first: $limit, type: "subTasks", member: $studentId) {
edges {
node {
id,
answer,
completion_date,
}
}
},
...
为了定义我想要从哪个用户获取数据,我使用:
componentWillMount = () => {
let {relay} = this.props;
relay.setVariables({
studentId: this.props.member.id
});
}
但是,这里的问题是,查询首先使用 initialVariables
中定义的 null
执行。我的后端实现具有这样的功能:如果 studentId
为 NULL,则它会返回所有成员的数据。
结果是上述查询执行两次,第一次使用所有成员,第二次使用给定的 memberId
。
这会扰乱我的 componentWillUpdate
中的其他内容。无论如何,我是否可以传递此变量并仅在第一个查询中获取该成员的数据?
谢谢。
最佳答案
通过路由向下传递变量,为您的应用程序准备好这些初始条件。
class TaskReviewRoute extends Relay.Route {
static paramDefinitions = {
studentId: {required: true},
};
static queries = {
task: () => Relay.QL`query { ... }`,
};
static routeName = 'TaskReviewRoute';
}
<Relay.RootContainer
Container={TaskReview}
route={new TaskReviewRoute({studentId: '123'})}
/>
关于reactjs - 继电器 - 在查询之前设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698582/