-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass in React Component to Relay Variables and get a fragment from it. #775
Comments
Thanks for your patience. Note that the An alternative approach here might be a container constructor:
|
Yeah that's actually the route I went after realizing my initial approach wasn't possible. Thanks! |
@ramsaylanier great, glad you got it working! |
@josephsavona sorry I realized that there is another issue. I'm calling the
This works, but I think its creating a new component everytime I switch between :page routes —For example, when I got to '/articles' and then to '/about'. When I switch back I get:
|
@ramsaylanier sorry for the delay getting back to you. have you tried memoizing the results of the |
I'm actually trying to go a different route based on the solution here: http://stackoverflow.com/a/35471007/2168061 Still having some issues though as noted in the comments. |
@josephsavona Basically, the problem I'm running into is that I'm using part of the initial query to determine the layout - so the first time it runs through and renders the correct component, it doesn't pick up the fragment. In a Page component I do this: _setLayout(){
const Layout = this.props.viewer.page.layout.meta_value || 'DefaultLayout';
const isDefault = Layout === 'DefaultLayout';
const isPostList = Layout === 'PostList';
this.props.relay.setVariables({
page: this.props.page,
isDefault: isDefault,
isPostList: isPostList
})
}
componentWillMount(){
this._setLayout()
} and the Page Container: const COMPONENTS = [
[DefaultLayout, 'isDefault'],
[PostList, 'isPostList']
];
export default Relay.createContainer(Page, {
initialVariables: {
page: null,
isDefault: false,
isPostList: false
},
fragments: {
viewer: (variables) => Relay.QL`
fragment on User {
${COMPONENTS.map( ([Component, layout]) => {
console.log('mapping');
const condition = variables[layout];
return Component
.getFragment('viewer', {page: variables.page})
.if(condition)
})},
page(post_name:$page) {
id
layout{
id
meta_value
}
},
settings{
id
uploads
amazonS3
}
}
`
}
}); But this gives me an
|
What is the render function of |
class Page extends React.Component{
_setLayout(){
const Layout = this.props.viewer.page.layout.meta_value || 'DefaultLayout';
const isDefault = Layout === 'DefaultLayout';
const isPostList = Layout === 'PostList';
this.props.relay.setVariables({
page: this.props.page,
isDefault: isDefault,
isPostList: isPostList
})
}
componentWillMount(){
this._setLayout()
}
componentDidMount(){
let animation = this.props.animation || PageAnimations.animateIn;
AnimateItem(this._page, PageAnimations.animateIn);
}
render(){
const { viewer, className } = this.props;
const { page } = viewer;
const Layout = Layouts[page.layout.meta_value];
return(
<div ref={ (c) => this._page = c } className={styles.base + ' ' + className}>
<Layout.Component viewer={viewer} page={page} layout={Layout}/>
</div>
)
}
} |
Was there ever a solution to this? I have the same use case and am not sure how to overcome this... |
I still haven't found a way to resolve this. 😢 |
Thanks for your patience on this issue. We've looked into it several times - I tried to repro, @steveluscher tried to repro, we hacked on it together - and it seems like there is a gap between our understanding of the problem and your actual use case (what we tried worked). I understand that it's a bit time consuming, but it would help to have a clear repro of the issue. Either a full Relay playground link, or a failing unit test, would help us clarify exactly what's happening and determine a solution. |
(Spring cleaning.) This one is nearly a year old now, so closing due to staleness. Thanks for filing the issue, and if you think it still exists in current Relay, please do send a repro so that we can investigate again. |
I'm querying a parent component that gets a "Layout" field, which contains a React Layout component. When the parent mounts, its goes through its query, gets the layout, and the updated its own Relay Variables to store the Component. It then renders the Component. But, there is currently no way to get a Fragment from the Component, since it technically doesn't exist when the query is first built.
Here's my parent component:
As you can see, the parent gets the layout field, it goes through a ShouldComponentUpdate check, and then calls a function that updates Relay Variables.
The text was updated successfully, but these errors were encountered: