Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukaswelte/32adea043d1605419d09c1d9adcb399a to your computer and use it in GitHub Desktop.
Save lukaswelte/32adea043d1605419d09c1d9adcb399a to your computer and use it in GitHub Desktop.
Pass relay variables down to other components
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
);
@lukaswelte
Copy link
Author

Also available on Relay Playground

@sibelius
Copy link

@lukaswelte great example

if I do a setVariables in the parent it will reload all children?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment