javascript - 在功能组件中,如何使用 react-redux connect 从 redux store 访问 props?
问题描述
在像下面这样的功能性反应组件中,您如何访问从 redux 存储发送过来的道具,类似于如何访问类组件的this.props
in 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);
解决方案
props 将作为功能组件的第一个参数传递。
const ComponentName = (props) => {
return (
<div>
<div>{ props.something }</div>
</div>
);
};
您可以在官方文档中找到更多详细信息https://reactjs.org/docs/components-and-props.html
推荐阅读
- c++ - 在 QT 中为 QPainter 设置填充颜色(不是描边颜色)
- python - 如何查找文本文件中的哪一行名称最多
- algorithm - 标签排序算法 - 无法让我的工作
- angular - 从 Firebase 检索用户信息并在应用中显示
- ios - iPhone 上的 Safari 不允许在字段中输入文本
- jquery - Wordpress 管理面板中的 jQuery 问题
- list - 根据另一列创建具有多个列的唯一名称的列表?
- javascript - 使用从 Alphavantage 返回的 axios 对象更新 reactjs 状态
- c# - 为什么这个 C# 脚本会导致在 Unity 中循环的游戏对象之间出现延迟峰值?
- android - Android Oreo+:AppWidget 点击不调用 JobIntentService.onHandleWork()