功能组件的 mapStateToProps [英] mapStateToProps for functional component

查看:50
本文介绍了功能组件的 mapStateToProps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有类似于 mapStateToProps 的函数,让我们可以将 Redux 状态连接到 React 中的功能组件?

Is there any function similar to mapStateToProps so that we can connect Redux state to the functional component in React?

将 state 作为 props 从父组件传递是唯一的解决方案吗?

Is passing the state as props from parent component the only solution?

推荐答案

您绝对可以将 mapStateToProps 与功能组件一起使用,就像使用类组件一样.

You can definitely use mapStateToProps with a functional component, the same way you would with a class component.

function MyComponent({ propOne }) {
  return <p>{propOne}</p>
}

function mapStateToProps(state) {
  return { propOne: state.propOne };
} 

export default connect(mapStateToProps)(MyComponent);

这篇关于功能组件的 mapStateToProps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆