繁体   English   中英

没有从 Redux 中的子组件获取道具

[英]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-reduxconnect()返回的高阶组件您不需要导出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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM