1

I'm bulding App with React Redux.

This is my reducer:

import { INCOME_LIST } from '../actionTypes'

import Immutable from 'immutable'

const initialUserState = {
  list: [{
                    id: 1,
                    label: 'List item 1'
                },
                {
                    id: 2,
                    label: 'List item 2'
                }]
}

const listReducer = function(state = [initialUserState], action) {
  switch(action.type) {
  case 'INCOME_LIST': 
         return Object.assign({}, state, { list: action.data });

  default: return state;
  }

}

export default listReducer

This is my Component:

import React, { Component, PropTypes } from 'react'
import { Link, browserHistory } from 'react-router'
import { connect } from 'react-redux'
import axios from 'axios'

class Income extends Component {
  constructor(props) {
    super(props)
  }
    }

render() {
 console.log(this.props.items)
        return (
            <div>
                test
            </div>
        )
    }

}

const mapStateToProps = function(store) {
  return {
   items: state.list
  };
}

export default connect(mapStateToProps)(Income);

Console says that: 'undefined' Why console.log(this.props.items) get undifined? How to right way to get a state from reducer? May be mistake there?

2 Answers 2

1

Three things I can see,

Firstly in mapStateToProps function you are defining the variable as store and using it as state. Change it to this

const mapStateToProps = function(state) {
  return {
   items: state.list
  };
}

Also after the constructor you have an extra } thats the reason you get an error when you place you console.log() in the render() function.

Also in your reducer you the way your are defining the initial state makes it an array. You should correct that like state='initialUserState

Complete code

Reducer

import { INCOME_LIST } from '../actionTypes'

import Immutable from 'immutable'

const initialUserState = {
  list: [{
                    id: 1,
                    label: 'List item 1'
                },
                {
                    id: 2,
                    label: 'List item 2'
                }]
}

const listReducer = function(state = initialUserState, action) {
  switch(action.type) {
  case 'INCOME_LIST': 
         return Object.assign({}, state, { list: action.data });

  default: return state;
  }

}

export default listReducer;

Component

class Income extends Component {
  constructor(props) {
    super(props)
  }


render() {
 console.log(this.props.items)
        return (
            <div>
                test
            </div>
        )
    }

}

const mapStateToProps = function(state) {
  return {
   items: state.list
  };
}

export default connect(mapStateToProps)(Income);
Sign up to request clarification or add additional context in comments.

7 Comments

do this but in console I get Undefined..... Why I can't get state form my reducer?
Try and console.log(state.list) inside the mapStateToProps function and see if it logs anything
Do that const mapStateToProps = function(state) { return { items: state.list }; console.log(state.list) } nothing happens
You have to log before the return statement
In you reducer you should assign initialstate like state=initialUserState change that and it should work
|
1

Your argument is named store, but you refer to it as state. Try:

const mapStateToProps = function(state) {
  return {
   items: state.list
  };
}

4 Comments

yes, you a right. Where I can make сonsole.log(items); for examle. In wich part code? because I try do it in render function and getting error. Sorry I'm new in react
Whatever returns from mapStateToProps is mapped to the component's props. Try console.log(this.props.items).
In wich part of code I can Paste it? because if I paste in render. I get error
ge undefined if, paste in render ((

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.