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 mapStateToProps problem

See original GitHub issue

Hi,

don’t know if this is the right place to ask, but I’ll try anyway. Is it wrong for me to expect that flow would not report a problem in the code below? It complains on the line accessing this.props.b about ‘b’ property not being found. I would expect that because of the mapStateToProps flow would know that the b property from the state is accessible in the props. Or am I doing something wrong?

Regards, Herman

// @flow
import React, {PropTypes} from 'react'; // eslint-disable-line no-unused-vars
import {connect} from 'react-redux';

type TMyState = {
  b: number
}

type Props = {
  a: number;
}

class TestCase extends React.Component<void, Props, void> {
  render(){
    let ret = this.props.b;
    return(
      <div>{ret}</div>
    );
  }
}

function mapStateToProps(state: TMyState) {
  return {
    b: state.b
  }
}

export default connect(mapStateToProps)(TestCase);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

16reactions
charlesoconorcommented, Sep 18, 2017

That’s too bad there is too so much boilerplate. It would be really nice if it could infer the prototypes that come out of connect. All of those types already exists in StoreState so it shouldn’t be too bad.

I’m stuck behind flow v0.41 mostly because of this issue.

11reactions
0x24a537r9commented, Aug 18, 2017

Our team (at Oculus, so Facebook) uses Flow and Redux, although Flow is not yet rich enough to support your use case automatically. That said, the workaround that we use is incredibly easy and very typesafe. A simplified example from our codebase for how we handle it:

import React from 'react';
import {connect} from 'react-redux';

import type {State as DeeplinkingState} from 'HomeDeeplinkingReducer';
import type {
  ReduxProps,  // Our type alias for dispatch() and other props attached by connect()
  State as StoreState,
} from 'HomePrimeStore';

type ReduxMappedProps = {
  deeplinking: DeeplinkingState,
};
type OwnProps = {
  error: ?Error,
};
type Props = ReduxMappedProps & ReduxProps & OwnProps;

const HomeDeeplinkCompletionMarker = (props: Props) => {
  ...
};

function mapStateToProps(
  state: StoreState,
  ownProps: OwnProps,
): ReduxMappedProps {
  return {
    deeplinking: state.deeplinking,
  };
}

export default connect(mapStateToProps)(HomeDeeplinkCompletionMarker);

Hope that helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux- mapStateToProps not working - Stack Overflow
My mistake was because I was doing wrong export/import of the component. export: export class MyComponent extends React.Component ... export ...
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
MapStateToProps not being called even after changing ...
I am very new to Redux and React not sure whether this is a issue or a usage question, pls excuse my ignorance....
Read more >
How to deal with failure in Redux connect - Medium
We proposed a solution based on an intermediary EitherComponent component that can fallback if we pass empty values.
Read more >
How to use the react-redux.connect function in react-redux
To help you get started, we've selected a few react-redux.connect examples, ... export default connect(mapStateToProps, mapDispatchToProps)(SelectTutorial);.
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