将 apollo 中 graphql 查询的默认值传递给 child

pass default value from graphql query in apollo to child

我正在尝试将 graphql 查询值从 parent 组件的状态传递到我的 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} />
)}}

添加

componentWillReceiveProps(nextProps) {
const defaultSelectedId = (nextProps.query && !nextProps.query.loading) ? 
           nextProps.query.defaultId : ''
     this.setState ({ 
         id: defaultSelectedId
     })

}