嗨,我可以访问容器上 prepareVariables 中的道具吗?
我有一个递归结构:
LocationList = Relay.createContainer(LocationList, {
fragments: {
location: () => Relay.QL`
fragment on LocationPart {
id, children {
id, hasChildren, value,
${LocationListItem.getFragment('location')}
}
}
`
}
});
LocationListItem = Relay.createContainer(LocationListItem, {
initialVariables: {
expanded: false
},
fragments: {
location: (variables) => Relay.QL`
fragment on LocationPart {
id, path, value, description, hasChildren,
${LocationList.getFragment('location').if(variables.expanded)}
}
`
}
});
在根上,我将第一级扩展为:
fragment on LocationPart {
${LocationListItem.getFragment('location', { expanded: true })}
}
我想保留整个状态并在以后恢复它。我已经涵盖的保留状态,我将带有状态的对象树传递给所有节点。所以我希望能够在prepareVariables中做到这一点:
prepareVariables() {
return { expanded: this.props.open[this.location.id] };
}
我尝试使用构造函数:
constructor(props) {
super(props);
if (props.open[props.location.id])
props.relay.setVariables({ expanded: true });
}
但随后中继抱怨提供的预期道具location
将由LocationList
中继获取数据。
这可能吗 ?