正确使用react redux connect
我是react redux的新手,我正在阅读这里的文档正确使用react redux connect,redux,react-redux,Redux,React Redux,我是react redux的新手,我正在阅读这里的文档 文档中说,connect的定义如下: 连接([mapStateToProps]、[mapDispatchToProps]、[mergeProps]、[options]) 但是我看到了这个示例代码 import React from 'react' import { connect } from 'react-redux' import { getData } from '../actions' class Content extends
文档中说,
connect
的定义如下:
连接([mapStateToProps]、[mapDispatchToProps]、[mergeProps]、[options])
但是我看到了这个示例代码
import React from 'react'
import { connect } from 'react-redux'
import { getData } from '../actions'
class Content extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.dispatch(getData())
}
render() {
const { data } = this.props; //This data is the same returned for fungetStore
return (
<div>
<h1> { data.text } </h1>
</div>
);
}
}
const fungetStore = store => {
return {
data: store //Return the content of the store
}
}
Content = connect(fungetStore)(Content)
export default Content
从“React”导入React
从“react redux”导入{connect}
从“../actions”导入{getData}
类内容扩展了React.Component{
建造师(道具){
超级(道具);
}
组件willmount(){
this.props.dispatch(getData())
}
render(){
const{data}=this.props;//此数据与为fungetStore返回的数据相同
返回(
{data.text}
);
}
}
const-fungetStore=store=>{
返回{
数据:存储//返回存储的内容
}
}
内容=连接(fungetStore)(内容)
导出默认内容
您可以在代码中看到,
fungetStore
以connect方式发送。但为什么要以这种方式使用connect?我们不认为您必须定义MapStateTrops
和/或mapDispatchToProps
?。文档中有我遗漏的东西?要连接的参数的名称是MapStateTrops
和mapDispatchToProps
。它们通常被称为mapState
和mapspatch
,但您可以根据需要调用函数
在本例中,fungetStore
是“mapState”函数的一个示例。无论它是被称为mapstatetops
,mapState
,fungetStore
,还是fred
,它都是一个函数,作为参数接收state
,并作为第一个参数传递给connect
此外,connect
的每个参数都是可选的。所有这些都是有效的:
connect(mapState, mapDispatch)(MyComponent) // use state and dispatch actions via functions
connect(mapState)(MyComponent) // use state
connect(null, mapDispatch)(MyComponent) // dispatch actions via functions
connect(null, null)(MyComponent) // dispatch actions via dispatch()
connect()(MyComponent) // dispatch actions via dispatch()
connect
的参数名称为MapStateTrops
和mapDispatchToProps
。它们通常被称为mapState
和mapspatch
,但您可以根据需要调用函数
在本例中,fungetStore
是“mapState”函数的一个示例。无论它是被称为mapstatetops
,mapState
,fungetStore
,还是fred
,它都是一个函数,作为参数接收state
,并作为第一个参数传递给connect
此外,connect
的每个参数都是可选的。所有这些都是有效的:
connect(mapState, mapDispatch)(MyComponent) // use state and dispatch actions via functions
connect(mapState)(MyComponent) // use state
connect(null, mapDispatch)(MyComponent) // dispatch actions via functions
connect(null, null)(MyComponent) // dispatch actions via dispatch()
connect()(MyComponent) // dispatch actions via dispatch()
可能的重复可能的重复