0
votes

I want to pass mapStateToProps value to next component while using withRouter so that i can maintain this.props.history also. Here is my code.

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

class MyApp extends React.Component {
    constructor ( props ) {
        super( props );
        this.state = {}
    }

    componentDidMount = () => {
        // it is printing empty array i.e. []
        console.log( " this.props.userDetails ", this.props.userDetails )
    }
    routeChange = () => {
        let path = `/someurl`;
        this.props.history.push( path );
    };

    render() {
        return <div>
            <button class="btn btn-secondary"
                data-target="#add"
                id="addnew"
                onClick={ this.routeChange }
            >
        </div>
    }
}
const mapStateToProps = state => {
    return {
        userDetails: state.UserDetailsReducer.userDetails
    };
};
export default withRouter( connect( mapStateToProps )( MyApp ) );

I tried both

export default connect( mapStateToProps )( withRouter( MyApp ) ); 

and

export default withRouter( connect( mapStateToProps )( MyApp ) );

and

export default compose(  withRouter,  connect( mapStateToProps ))( App );

but the value is empty array. Here is another file.

import React, { Fragment, useState, useEffect } from 'react';
import { NavLink } from 'react-router-dom';
import { connect } from 'react-redux';
import UserDetails from '../store/Actions/UserDetails'; 

const NavigationBar = (props) => {
    useEffect(() => {
        fetch(`###REACT_APP_PLATFORM_URL###/access/userinfo`, requestOptions)
            .then(response => {
                if (response.status != 200) { setisError(true) }
                return response.json();
            })
            .then((findresponse) => {
                if (findresponse.msg) { setstatus(findresponse.msg) }
                else {
                    setcheckName(findresponse);
                    props.getUserDetails(findresponse) 
                }
            })
            .catch(error => { });
    }, []); 
};
const mapStateToProps = (state) => {
    return {
        userDetails: state.UserDetailsReducer.userDetails
    }
}
const mapDispatchToProps = (dispatch) => {
    return {
        getUserDetails: (user) => dispatch(UserDetails(user))
    }
}
export default connect(mapStateToProps, mapDispatchToProps)(NavigationBar);
The fact that it's an empty array and not undefined tells me it's working. What does your state look like?Adam
its not empty array, it has values it was initialized by const mapDispatchToProps = (dispatch) => { return { getUserDetails: (user) => dispatch(UserDetails(user)) } } in another component.Ankit Giri
Are you mutating the array in redux by any chance which is causing it not to result in a re-render of this component when it's updated? Post your reducer code.Adam
@Adam import { USERDETAILS } from '../Types/ActionTypes'; const InitialState = { userDetails: [] } const UserDetailsReducer = (state = InitialState, action) => { switch (action.type) { case USERDETAILS: return Object.assign({}, state, { userDetails: action.payload }); default: return state; } } export default UserDetailsReducer;Ankit Giri
@Adam export default function rootReducer(asyncReducers) { return combineReducers({ ToolsDetails, CartridgeDetails, UserReducer, PipelineDetails, UserDetailsReducer, ProjectDetailsReducer, ...asyncReducers }) }Ankit Giri