question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-redux] DefaultProps support

See original GitHub issue

I was trying to add connect to a component that has defaultProps and I had a hard time getting it right, even thought I could.

One thing I noticed while looking for ways to do it, is that there is an issue on the current tests: https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/react-redux_v5.x.x/flow_v0.89.x-/test_connect.js#L10

That line should be passthroughWithDefaultProp: number, (not optional) because that component has passthroughWithDefaultProp as a defaultProp. When trying to use passthroughWithDefaultProp inside that component, even though that prop is always a number (guaranteed by defaultProp) Flow will complain that it can be a number or undefined, which is not ideal.

Also note that on this Flow documentation example foo is declared as number and not an options `number: https://flow.org/en/docs/react/hoc/#toc-exporting-wrapped-components

This try-flow link shows the problem I’m trying to point out: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAtgBzgJwBdkwBDAZzACUBTUgY2KnzizAHJ87H2BudYMDABJAHb0CXRgC4wAV3IZRAczBwchDHFGkYYKATJgAJjShKaxk2dJyYhAAosc5dIQCeOGmCfryARjAAXjAAbwAfVDAwQTAAFQALDEpyBLg7K1E4YgAjb3VNbV0wPPpbcnyoMEIE71NzUUtrKFt7XxcAOiiwAA8AfllyQnwlZVRwgF9+VHoYCkoADTAaHsIaUWNKWgZCDoBhVjxG0UIAHnaAgD4w7qHSTXpm1sdnShDQ7uie2XZ2bonulwNjR8AAKACUN2i0ViIJY+E+YC4hDk+FE1SS5A6OFeHR6YAA1Bw+P9UADULEDvgpIRZAA5bLeNJIABi8CQCG07CYpAwegwVQQ3g8XjAGGIFDAAAN3INhqMpWAEOKEhi6jY7C8-G5PN4LgAmYJhSLRWVgIYjFTjKboWbzMAATWWq3Wm2o3F2B1w2nWZwN1w+0TuDyemouRsD0LNv1J0SBpjBkMjMKEWWW1IIAEJEcjUeiaslsbj3ITifxogDybEGWswMywGzEEquTy+XWQTQADQlOSt+xK2rorhYOAAN1G0vqzwuitBFW8jVWztIuBgNHBqFBpydwEusm2jA6AFE11hfacRTQ4FUHZdwdNYgApBTEVKICf1xtIFp8yjKmpGFk6IFFoOh6Di6hKiq6TEFKU5hq8Uo6qKFwAMwRiaYAAF5ypaYyTNMdrkJQABazprBsWwevshw+ic5yvKhAaAq6IIQlC0K5miGKFhBnRYaWvzlmAlYCEIR4ZgiW5kbu+7USeNBnvRl7XmAJF3rwQA

Here is how I made it work, but maybe there is a way to avoid having to use React.Config ourselves?:

import * as React from 'react';

type OwnProps = {|
  x: number
|};

type Props = {|
  ...OwnProps,
  // no state props
|};

class Comp extends React.Component<Props> {
  static defaultProps = { x: 3 };
  ...
}

type DefaultProps = {| x: number |};
type Config = React.Config<Props, DefaultProps>;
type OwnConfig = React.Config<OwnProps, DefaultProps>;

connect<Config, OwnConfig, _, _, _, _>()(Comp);

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
AndrewSouthpawcommented, Feb 14, 2019

Nice work @fabiomcosta, would you mind documenting this somewhere useful? Maybe in the react-redux libdef?

2reactions
jbrown215commented, Feb 11, 2019

In the way Flow models react, Props should be the type of this.props and Config is the type of the Props with all defaultProps marked optional. The way you handled it in your example at the bottom of this issue is exactly correct!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare ReactJS default props when using Redux?
Here's how you can declare default props when using the ES6 class syntax for creating ReactJS components: class ContainerComponent extends ...
Read more >
React-redux connect and default props · Issue #3190 - GitHub
My understanding is that default properties on a component should be required not optional when declaring Flow type definitions ...
Read more >
A complete guide to React default props - LogRocket Blog
Cover three ways to implement default props in React and provide default values for props that are not required by the component.
Read more >
React: Everything about Default Props | by Chidume Nnamdi
In this post, we'll learn all about default props in React. It will help us not only become better developers but to provide...
Read more >
ReactJS defaultProps - GeeksforGeeks
The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found