0
votes

I just started learning Redux and having a tough time managing state. The code snippets are mentioned below :

TestComponent.js

class TestComponent extends React.Component {
  componentDidMount() {
  // Mounts component
  }

  render() {
    <div>
        {console.log("Inside div")}
        {console.log(this.props.item2)} // Get undefined here
          <button onClick = {() => create(1, this.props.item2)}></button> 
    </div>
  }

  const dispathToPropertyMapper = dispatch => {
    return {
      create: (id, item2) => { 
        // Does somthing
      }

  const stateToPropertyMapper = state => {
    return {
      item1: state.state1.item1,
      item2: state.state2.item2
    };
  };

    myReducer.js
const initialState = {
      item1: [],
      item2: "Sample Text"
    };

const myReducer= (state = initialState, action) => {
  console.log(action.type);
   switch (action.type) {
     return state;
   }

I have done all the exports and imports correctly. I am getting the props for item 1 which is an empty array but I am getting undefined for the prop item2. This is my first time using redux and spent a lot of time on this. Please help Thanks in adv

Can you share the code where you are doing the 'connect' method? like : connect(stateToPropertyMapper, dispathToPropertyMapper)(TestComponent) - Neha Sharma
Can you share your import and export statements as well? - Jawi
Hi Neha, The connect method is similar to what you have mentioned export default connect( stateToPropertyMapper, dispatchToPropertyMapper )(TestComponent); - reactdontact
import { connect } from "react-redux"; import React from "react"; - reactdontact
Have you tried defining the state and dispatch matching functions as functions rather than class methods? - Jawi