Ad
How To Make Props.relay.setVariables Update My Data?
I'm trying to use a search component's 'onChange' to filter results using this.props.relay.setVariables but nothing gets re-rendered. I'm also hoping that when it does filter, that it will still use the Shuffle component. We are making one trip to the DB up front, and all these subsequent relay queries are coming from a cache (not sure if that is pertinent).
const GamesScreen = React.createClass({
propTypes: {
viewer: PropTypes.object,
},
search (e) {
let searchStr = e.target.value;
this.props.relay.setVariables ({ query: searchStr}, onFetch);
},
onFetch (){
// does something need to happn in here to actually filter and re-render?
},
getSubjects ()
{
return this.props.viewer.subjects.edges;
},
render ()
{
const games = this.getSubjects().map(game => {
return (
<div className="Game-container" key={game.node.name}>
<Game game={game.node} />
</div>
);
});
return (
<div className="GamesScreen">
<TopContainer>
<TopBar viewer={this.props.viewer} />
</TopContainer>
<MidBar viewer={this.props.viewer} />
<input type="search" placeholder="Search" onChange={this.search} />
<div className="GamesScreen-container">
<Shuffle duration={300} fade>
{games}
</Shuffle>
</div>
</div>
);
},
});
export default Relay.createContainer(GamesScreen, {
initialVariables: {
query: '',
},
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
subjects(query: "", first: 5)
{
edges {
node {
name
}
}
},
${TopBar.getFragment('viewer')},
}
`,
},
});
Ad
Answer
You just need to use the variable in your fragment. In GraphQL, variables are used by prepending $
to the variable name, kinda like bash or whatever:
export default Relay.createContainer(GamesScreen, {
initialVariables: {
query: '',
},
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
subjects(query: $query, first: 5) // <-- USE $query TO MODIFY FRAGMENT AT RUNTIME
{
edges {
node {
name
}
}
},
${TopBar.getFragment('viewer')},
}
`,
},
});
For more details, see the docs about Relay Containers and variables here
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad