首页 > 解决方案 > 在功能组件中,如何使用 react-redux connect 从 redux store 访问 props?

问题描述

在像下面这样的功能性反应组件中,您如何访问从 redux 存储发送过来的道具,类似于如何访问类组件的this.propsin componentDidMount(),如下面的评论所示?

import React from 'react';
import { connect } from "react-redux";
import * as actions from "../../actions";

const ComponentName = () => {
    // componentDidMount() {
    //    this.props;
    // }

    return (
        <div>
            <div>???</div>
        </div>
    );
};

function mapStateToProps(state) {
    return { state };
}

export default connect(mapStateToProps, actions)(ComponentName);

标签: javascriptreactjsreduxreact-redux

解决方案


props 将作为功能组件的第一个参数传递。

const ComponentName = (props) => {
    return (
        <div>
            <div>{ props.something }</div>
        </div>
    );
};

您可以在官方文档中找到更多详细信息https://reactjs.org/docs/components-and-props.html


推荐阅读