Prop in relay training.

Hi, can I access the details in the prepareVariables file in the container?

I have a recursive structure:

LocationList = Relay.createContainer(LocationList, {
    fragments: {
        location: () => Relay.QL`
            fragment on LocationPart {
                id, children { 
                    id, hasChildren, value,
                    ${LocationListItem.getFragment('location')} 
                }
            }
        `
    }
});
LocationListItem = Relay.createContainer(LocationListItem, {
    initialVariables: {
        expanded: false
    },
    fragments: {
        location: (variables) => Relay.QL`
            fragment on LocationPart {
                id, path, value, description, hasChildren,
                ${LocationList.getFragment('location').if(variables.expanded)}
            }
        `
    }
});

And in the root, I will analyze the first level at:

fragment on LocationPart {
    ${LocationListItem.getFragment('location', { expanded: true })}
}

I want to save all the state and restore it later. The save state that I embraced, and I pass a tree of objects with state to all nodes. Therefore, I would like to be able to do this in prepareVariables:

prepareVariables() {
    return { expanded: this.props.open[this.location.id] };
}

I tried using the constructor:

constructor(props) {
    super(props);
    if (props.open[props.location.id]) 
        props.relay.setVariables({ expanded: true });
}

but then the relay complains about the Expected Scrolling locationsupplied in LocationListto retrieve the relay data.

Is it possible?

+4
source share
2 answers

, / , Relay . LocationListItem , . , expanded , .

GraphQL Relay: , ( Relay ). :

  • , , / .
  • , setVariables componentDidMount, (, prepareVariables).
+2

- prepareVariables , . , .

+4

Source: https://habr.com/ru/post/1619744/


All Articles