我想从子组件中的 Redux 获取 props。但 {this.props} 是空对象。我正在使用react-redux connect来获取 Prop 。它在父组件中工作,我们可以传递给子组件来获取 Prop ,但我需要从子组件获取
登录
```import React, { Component } from 'react'
import { Test } from './Test'
export default class Login extends Component {
render() {
return (
<div>
<Test hi=""/>
</div>
)
}
}```
测试
import React, { Component } from 'react'
import { connect } from 'react-redux'
export class Test extends Component {
render() {
console.log(this.props)
return (
<div>
vddfff
</div>
)
}
}
const mapStateToProps = (state) => ({
})
const mapDispatchToProps = (dispatch) => {
return {
selectedData: (val) => {
console.log("object")
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Test)
最佳答案
这里的问题是您尝试使用您编写的测试组件,而不是由 react-redux
的 connect()
返回的高阶组件>
您不需要导出 Test
类。
在login.js文件中,使用
从“./Test”导入测试
而不是
从“./Test”导入{Test}
关于javascript - 未从 Redux 中的子组件获取 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59971452/