Reputation: 5186
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
Reputation: 3687
Add
componentWillReceiveProps(nextProps) {
const defaultSelectedId = (nextProps.query && !nextProps.query.loading) ?
nextProps.query.defaultId : ''
this.setState ({
id: defaultSelectedId
})
}
Upvotes: 1