react-redux icon indicating copy to clipboard operation
react-redux copied to clipboard

Connect doesn't warn if same property is sent from both parent and state

Open ynonp opened this issue 10 years ago • 5 comments

The default merge method in connect:

const defaultMergeProps = (stateProps, dispatchProps, parentProps) => ({
  ...parentProps,
  ...stateProps,
  ...dispatchProps
})

Will just overwrite existing parent props with state props when same key is used.

IMHO it'd be better to show a warning to console, saving developers debugging time & effort

ynonp avatar Sep 24 '16 15:09 ynonp

Not sure I agree with this, as that's the whole point of the mergeProps function: It lets you customize this default behavior (almost always for this exactly kind of problem).

timdorr avatar Sep 25 '16 02:09 timdorr

I mostly agree, but given that it would be a dev-only check, I'm not sure it's actually a bad idea. Or rather, it seems like a decent idea, and I don't see any particular downside to it.

markerikson avatar Sep 25 '16 02:09 markerikson

BTW, since this is dev-only, I'll revert my previous comment and say this is an OK idea. 😄

timdorr avatar Sep 27 '16 16:09 timdorr

Are there any valid use cases where a developer wants it to just overwrite and not complain? In that case, the developer has to do one of:

  • Live with the warning in dev mode.
  • Implement their own mergeProps that doesn't warn, but connect() contains an optimization when mergeProps is omitted, avoiding an extra shallow equality check. This means their app will run slightly slower. It's probably unnoticeable except for edge cases involving a high frequency of dispatches that result in that component's mapStateToProps producing new results, or that component receiving new props from parent at a high frequency.
  • If they do hit that edge case for performance, then replace that particular usage of connect with connectAdvanced and be responsible for their of memoization of props.

I'm OK with all that, but now we probably need to document this, probably in some advanced optimization section.

jimbolla avatar Sep 28 '16 16:09 jimbolla

Closing after merged PR #508

ynonp avatar Oct 03 '16 20:10 ynonp