0

I have two independent components called login and home. I have used mapStateToProps in login component to pass data.

function mapStateToProps(state) {
  return { loginData: state.agent };
}

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

A button click from the login component will navigate us to the home page. I want to get and display all the 'loginData' values in the home component. I have tried this.props.loginData in home comp. But didn't get the store data. Any suggestions on this?

4
  • 2
    You need to connect home component to the store as well. Commented Dec 18, 2018 at 11:05
  • @YuryTarabanko - I'm new to react. I have used <Provider store={ store }> to wrap my components. Do I need to do anything from the home component? Commented Dec 18, 2018 at 11:10
  • 1
    I mean you need to do the same thing you did to login. connect(mapStateToProps)(Home) Commented Dec 18, 2018 at 11:12
  • @YuryTarabanko - Now I'm able to get the data in mapStateToProps function of the home component. How can I get this data to the componentDidMount method? Commented Dec 18, 2018 at 11:28

3 Answers 3

1

To access the this.props.loginData in your homepage component, even there you need to use mapStateToProps like the way you have used in login component.

function mapStateToProps(state) {
  return { loginData: state.agent };
}

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

then you will able to access this.props.loginData

If you want to access the Redux store data in any component you need to use connect and make use of mapStateToProps to connect the store values to your props.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It works. Could you please tell me how to get this loginData in componentDidMount of home component. When I'm trying with this.props.loginData -- 'Undefined' it what I'm getting
I am using connect export default connect(stateToProps)(AuthorizedRoute); in my main route where all the routes are mounted, how can I access redux data on store on other components? Like in HomePageComponent?
0

My suggestion would be to fire an action to fill the redux store with the details the user has entered after being logged in. After that can use mapStateToProps in the home page to get the details from the redux store.

1 Comment

I have already used action and a reducer. I'm able to get the store data in the login page itself, but not able to pass it to another component.
0

You can pass in data via state property

If you are using history.push

How to pass params with history.push in react-router v4?

similar with redirect

https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Redirect.md

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.