Skip to content

Using Array type for fragment variable #1357

Closed
X-oss-byte/next.js
#1421
@josercruz01

Description

@josercruz01

I've run into an issue where the RelayContainer resets its current fragment variables state and forgets variables I've already set via this.props.relay.setVariables.

I've tracked down the problem to the method RelayContainer#resetPropOverridesForVariables, which is called inside the RelayContainer#componentWillReceiveProps. This method resets the container's fragment variables state to the initial variables if any of the new props has changed.

So basically the method checks all fragment variables and resets the state if at least one changed, but in my case it always gets reset because one of those variables is an array which never returns true for === comparison.

You can see below for my prepareParams method which converts the query param into an array. My question is, would it make sense to provide a hook for custom equality comparison (maybe check if the prop has an equals method, that way I could send in an Immutable List) or should I not be converting to array at all and send a string to the server?

I would rather not send a string if possible, just so I don't have to do extra work on the client and server to parse the string to array, any recommendations?

const prepareScreeningParams = function(params, { location }) {
  const { query } = location;
  const variables = Object.assign({}, params, {
    keywords: query.keywords,
    referrer: query.referrer,
    university: query.university,
    interval: parseInt(query.interval, 10),

    // This is now an array of ints
    currentStageIds: query.currentStageIds ?
      _.flatten([query.currentStageIds]).map(x => parseInt(x, 10)) : null
  });
  return _.mapValues(variables, x => x || null);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions