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.

Cannot get to redux store using mergeProps(stateProps, dispatchProps, ownProps)

See original GitHub issue

Hi,

I have 3 container components implemented as simple drop down lists presentation components. Options available to choose from are calculated based on choices in previous components.

Components and data flow:

OrganisationContainer -> select an org -> load available groups into -> OrganisationGroupsContainer -> select a group -> load available contacts into -> ContactsContainer

My problem occurs when I need access to the first container’s chosen value (via redux store) in the second container so that I can calculate options for third container.

Sample code:


// My redux store in its initial state is:
{ 
  orgName: '',
  orgsEnabled: ['NSA', 'FBI', 'MI6'],
  orgGroup: '',
  availableGroups: [],
  orgGroupContact: '',
  availableContacts: []
}

// Organisation container
const mapStateToProps = (state) => {
    return {
        orgName: state.orgName
        orgsEnabled: state.orgsEnabled
    }
}

const mapDispatchToProps = (dispatch) => {
    return {
        onChange: (newOrg) => {
            dispatch(setOrgName(newOrg))
            dispatch(requestOrgGroups(newOrg))
            dispatch(setOrgGroup(''))
            dispatch(setContact(''))
        }
    }
}

const OrganisationContainer = connect(
    mapStateToProps,
    mapDispatchToProps
)(OrgComponent)

export default OrganisationContainer 

// OrganisationGroups container
const mapStateToProps = (state) => {
    return {
        orgGroup: state.orgGroup,
        availableGroups: state.availableGroups
    }
}

const mergeProps = (stateProps, dispatchProps, ownProps) => {
    return Object.assign({}, ownProps, {
        orgGroup: ownProps.orgGroup,
        availableGroups: stateProps.availableGroups,
        onChange: (newGroup) => {
            dispatchProps.setGroup(newGroup)
            dispatchProps.setContacts('')

            // PROBLEM here: stateProps.orgName returns undefined
            dispatchProps.requestContacts(stateProps.orgName, newGroup)
        }
    })
}

const actions = { setGroup, setContacts, requestContacts }

const OrganisationGroupsContainer  = connect(
    mapStateToProps,
    actions, 
    mergeProps
)(OrganisationGroupsComponent)

export default OrganisationGroupsContainer  


// app
const App = () => (
    <div>
        <Header/>
        <OrganisationContainer />
        <OrganisationGroupsContainer  />
        <ContactContainer />
    </div>
)

export default App

// main
let store = createStore(OrgReducer)

render(
    <Provider store={store}>
        <App/>
    </Provider>,
    document.getElementById('app')
)

I have left out code for the ContactContainer, reducers, createStore, actions and other boilerplate as testing and stepping through shows that it all works fine – right code gets called at the right time.

The problem is stateProps.orgName in mergeProps : this is returning undefined even though the value is clearly set in the store.

I am doing my best to follow recommendations in #211 i.e. not using mapDispatchToProps and using mergeProps but still having no luck.

All containers are connected and if I simulate by replacing:

 dispatchProps.requestContacts(stateProps.orgName, newGroup)

//with

 dispatchProps.requestContacts('NSA', newGroup)

… it all works out fine.

Am I doing something obviously wrong? Should I be using some other pattern/paradigm?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Feb 12, 2016

I am also closing the issue because this appears to be a usage question rather than a genuine issue with the library. Nevertheless feel free to post a link to the project reproducing it and I can try to take a look.

0reactions
gaearoncommented, Feb 12, 2016

Please provide a complete project reproducing the problem with instructions to reproduce it. It is hard to help because I don’t really understand what the code should be doing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does function used in mergeProps not update when ...
I have a state nextBoxID in the redux store and action + reducer that updates a boxID when a button is pressed. Issue...
Read more >
How to deal with failure in Redux connect ? | by Guillaume Wuip
Redux's connect is ubiquitous in the classic Redux/React application but ... mergeProps: (stateProps, dispatchProps, ownProps) => finalProps.
Read more >
React Redux connect(): When and how to use it
How to connect an app to the Redux store; When to use connect() in Redux ... mergeProps(stateProps, dispatchProps, ownProps) => props.
Read more >
Mastering mergeProps in Redux - Peanut Butter JavaScript
You can use mergeProps to simply connected components in your Redux application. When you connect a component using Redux, most people are ...
Read more >
Connect | React Redux
mergeProps ?: (stateProps, dispatchProps, ownProps) => Object ​. If specified, defines how the final props for your own wrapped component are ...
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