简体   繁体   中英

How to call mapStateToProps in react component to get Redux stores

I am trying to get the Redux state that is stored to be set to a component's props. My components implementation is below

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

class Characters extends Component {
    render() {
        console.log('props', this.props);
        return(
            <div>
                <h4>Characters</h4>
            </div>
        )
    }
}

const mapStateToProps = state => {
    return {
        characters: state.characters
    }
}

export default connect()(Characters);

I have my reducers set up like following. Now my console.log() is printing out a dispatch object not the props. I am not sure what else I need to do to set the component's props. I have tested my reducers and they seem to work fine. I am having trouble setting the component's props to the redux store. Below is my main reducer.

import { combineReducers } from 'redux';
import characters from './characters_reducer';
import heroes from './heroes_reducer';

const rootReducers = combineReducers({
    characters,
    heroes
});

export default rootReducers;

Not sure what am I doing wrong here. A little help and a hinge to the right direction would help a lot. Thanks :)

您忘记传递mapStateToProps进行连接:

export default connect(mapStateToProps)(Characters);

您需要将actionCreators和mapStateToProps函数传递给connect函数,以便能够从组件访问商店:

export default connect(mapStateToProps)(Characters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM