1
votes

I want to get props from Redux in the child component. But {this.props} is empty object. I am using react-redux connect to get the props. It is working in parent component and we can pass to child component to get the props but I need to get from child component

Login

```import React, { Component } from 'react'
import { Test } from './Test'

export default class Login extends Component {
    render() {
        return (
            <div>
                <Test hi=""/>
            </div>
        )
    }
}```

Test

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)
2
Because you missed constructor(props) { super() }. There are a good explanation about it overreacted.io/why-do-we-write-super-props - demkovych
You props do nothing with redux. - demkovych
@demkovych I added constructer. but still the same. One more thing I can see this.props.hi from parent to child but not this.props.selectedData - hemanth
just use const mapDispatchToProps = { selectedData: selectedDataAction }; where selectedDataAction is your action (from redux) - demkovych
You might want to use dispatch in the body of selectedData method. - technophyle

2 Answers

0
votes

The problem here is that you are trying to use the Test component you wrote instead of the Higher order component which is being returned by the connect() from react-redux You don't need to export the Test class.

In login.js file, use

import Test from './Test'

instead of

import {Test} from './Test'

See it in action here. https://codesandbox.io/s/sample-test-app-q5srf

0
votes

You used the wrong component. In login you need to import:

import Test from './Test'

You imported Test wich is not connected to redux (that pushes the redux props).