Kevin Danikowski
Kevin Danikowski

Reputation: 5186

pass default value from graphql query in apollo to child

I am trying to pass graphql query value to my React component from the parent component's state. My method isn't work working because it's loading and isn't called again after it is done loading. Any recommendations on how to do this?

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} />
)}}

Upvotes: 0

Views: 691

Answers (1)

simbathesailor
simbathesailor

Reputation: 3687

Add

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

}

Upvotes: 1

Related Questions