1

I am trying to access the name field as defined in the initial state of my reducer. At the moment, this.props is returning undefined.

My reducer:

import { combineReducers } from 'redux';

const INITIAL_STATE = {
    name: "Test"
}

const userReducer = (state = INITIAL_STATE, action) => {
    switch (action.type) {
      default:
        return state
    }
};

export default combineReducers({
  user: userReducer,
});

The component being rendered (this logs "Props: undefined"):

const AddName = ({ navigation }) => {

    ...

    console.log("Props: ", this.props)

    return (
        <>
        ...
        </>
    )
}

mapStateToProps = (state) => {
    return{
      user : state.user
    };
}

export default connect(mapStateToProps, null)(AddName)

Creating the redux store and provider:

...
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducer from './src/reducers/userReducer';

const store = createStore(reducer)

const AppContainer = () => 
    <Provider store={store} >
        <App />
    </Provider>


AppRegistry.registerComponent(appName, () => AppContainer);
3
  • It's a functional component, there is no this but you can access props - change const AddName = ({ navigation }) => { to const AddName = (props) => { and then access the props object - otherwise you are destructuring the props in the function definition. Commented Dec 20, 2019 at 23:24
  • reactjs.org/docs/… Commented Dec 20, 2019 at 23:26
  • @madebydavid Doh, thank you! I ended up using the new React Hooks in react-redux. thoughtbot.com/blog/using-redux-with-react-hooks Commented Dec 21, 2019 at 1:46

3 Answers 3

1

You are using a functional component and this keyword doesnt apply to functional components, rather it applies it to class Components.

Change your component as below :

class AddName extends React.Component {

    ...

    console.log("Props: ", this.props)

    render(){
    return (
        <>
        ...
        </>
    )

     }

}

mapStateToProps = (state) => {
    return{
      user : state.user
    };
}

export default connect(mapStateToProps, null)(AddName)

hopeit helps. feel free for doubts

Sign up to request clarification or add additional context in comments.

Comments

0

You are using a function Component so you should use props in this way :

const AddName = (props) => {

...

console.log("Props: ", props)

return (
    <>
    ...
    </>
 ) 
}

Comments

0

You should use class components instead of the function component. Read more about that.

https://codeburst.io/react-js-understanding-functional-class-components-e65d723e909

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.