[英]Not getting props from child component in Redux
我想在子组件中从 Redux 获取道具。 但是 {this.props} 是空对象。 我正在使用 react-redux connect 来获取道具。 它在父组件中工作,我们可以传递给子组件以获取道具,但我需要从子组件获取
```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)
您使用了错误的组件。 在登录时,您需要导入:
import Test from './Test'
您导入的 Test 没有连接到 redux (这会推动 redux 道具)。
这里的问题是您正在尝试使用您编写的 Test 组件而不是由react-redux
的connect()
返回的高阶组件您不需要导出Test
类。
在 login.js 文件中,使用
import Test from './Test'
代替
import {Test} from './Test'
在这里查看它的实际效果。 https://codesandbox.io/s/sample-test-app-q5srf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.