Reactjs 将apollo中graphql查询的默认值传递给子级

Reactjs 将apollo中graphql查询的默认值传递给子级,reactjs,graphql,react-apollo,Reactjs,Graphql,React Apollo,我试图将graphql查询值从父组件的状态传递给React组件。我的方法无法工作,因为它正在加载,加载完成后不会再次调用。有没有关于如何做到这一点的建议 class Parent extends Component { constructor(props) { super(props) const defaultSelectedId = (this.props.query && !this.props.query.loading) ?

我试图将graphql查询值从父组件的状态传递给React组件。我的方法无法工作,因为它正在加载,加载完成后不会再次调用。有没有关于如何做到这一点的建议

class Parent extends Component {
   constructor(props) {
     super(props)
     const defaultSelectedId = (this.props.query && !this.props.query.loading) ? 
           this.props.query.defaultId : ''
     this.state = { 
         id: defaultSelectedId
     }
   render() {
      return(
<Child selectedId={this.state.id} />
)}}
类父级扩展组件{
建造师(道具){
超级(道具)
const defaultSelectedId=(this.props.query&&!this.props.query.loading)?
this.props.query.defaultId:“”
this.state={
id:defaultSelectedId
}
render(){
返回(
)}}
添加


您也可以从构造函数中删除它。初始化只发生一次,当初始化发生时,它将是
load=true
。还可以添加一个检查以查看它是否实际发生了更改。
(nextrops.query&&!nextrops.query.loading&&nextrops.query.defaultId!==this.state.id)
.P.S.除非您正在处理子组件中的空字符串
id
,或者您的渲染比您正在显示的更复杂,否则在加载为真时,您应该阻止渲染它。谢谢你们,这非常有帮助,现在可以工作了。虽然this.setState=…应该是this.setState(。。。
componentWillReceiveProps(nextProps) {
const defaultSelectedId = (nextProps.query && !nextProps.query.loading) ? 
           nextProps.query.defaultId : ''
     this.setState ({ 
         id: defaultSelectedId
     })

}