Last active
May 4, 2019 20:50
-
-
Save lukaswelte/32adea043d1605419d09c1d9adcb399a to your computer and use it in GitHub Desktop.
Pass relay variables down to other components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChildComponent extends React.Component { | |
render() { | |
const {hello} = this.props.greetings; | |
return <h1>{hello}</h1>; | |
} | |
} | |
const ChildContainer = Relay.createContainer(ChildComponent, { | |
initialVariables: { | |
name: 'A', | |
}, | |
fragments: { | |
greetings: () => Relay.QL` | |
fragment on Greetings { | |
hello(name: $name), | |
} | |
`, | |
} | |
}); | |
class ParentComponent extends React.Component { | |
render() { | |
// *** have to pass `name` prop here *** | |
this.props.relay.setVariables({ | |
name: "Name", | |
}); | |
return ( | |
<ChildContainer | |
greetings={this.props.greetings} | |
name={this.props.relay.variables.name} | |
/> | |
); | |
} | |
} | |
const ParentContainer = Relay.createContainer(ParentComponent, { | |
initialVariables: { | |
name: 'C', | |
}, | |
fragments: { | |
greetings: ({name}) => Relay.QL` | |
fragment on Greetings { | |
${ChildContainer.getFragment('greetings', {name})}, | |
} | |
`, | |
} | |
}); | |
class Route extends Relay.Route { | |
static routeName = 'Hello'; | |
static queries = { | |
greetings: () => Relay.QL` | |
query GreetingsQuery { | |
greetings, | |
} | |
`, | |
}; | |
} | |
ReactDOM.render( | |
<Relay.RootContainer | |
Component={ParentContainer} | |
route={new Route({name: 'C'})} | |
/>, | |
mountNode | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lukaswelte great example
if I do a
setVariables
in the parent it will reload all children?