Unable to access Redux array state from component









up vote
0
down vote

favorite












I'm trying to access my Redux groceries array from my grocery list component, and when I try to access the state with this.props.groceries, it returns 'undefined'. I'm still trying to wrap my head around some Redux concepts, but I think I'm really close. In my store.js, I'm logging



store.subscribe(() => 
console.log('store changed', store.getState());
);


And getState() is displaying my correct groceries array, with all the groceries inside it. I'm just not sure how to access this state from my groceries list component. Thanks!



Overview of my GroceryList component:



 import connect from "react-redux";
import bindActionCreators, createStore from 'redux';
import * as groceryActions from '../../redux/actions/grocery-actions';

class GroceryList extends Component
constructor(props)
super(props);
this.state =
;

addGroceryToList()
this.props.addGrocery(newGroceryItem);
console.log(this.props.groceries); //Logs undefined

render()
return(
//something
)



const mapStateToProps = (state) => (
groceries: state.groceries.groceries
);

const mapDispatchToProps = dispatch =>
bindActionCreators(
addGrocery: groceryActions.addGrocery, ,
dispatch
)

export default connect(mapStateToProps, mapDispatchToProps)(GroceryList);


Grocery action:



let groceryIndex = 0;

export const addGrocery = grocery => dispatch =>
dispatch(
type: 'ADD_GROCERY',
id: groceryIndex++,
grocery
);
;


Grocery reducer:



export const groceries = (state = , action) => 
switch (action.type)
case "ADD_GROCERY":
return [
...state,
grocery(action.grocery, action),
];
default:
return state



export const grocery = (state, action) =>
switch (action.type)
case "ADD_GROCERY":
return
id: action.id,
grocery: action.grocery,
;
default:
return state




Reducer combiner:



import combineReducers from 'redux';

import groceries from './grocery-reducer';

const reducer = combineReducers(
groceries: groceries,
);

export default reducer;


Store



import createStore, applyMiddleware, compose from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducers';

if (typeof window === 'undefined')
global.window =


const enhancer = compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__
? window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f
);

/* eslint-disable no-underscore-dangle */
const store = createStore(
reducer,
, // initial state
enhancer
)

store.subscribe(() =>
console.log('store changed', store.getState());
);

/* eslint-enable */

export default store


App.js



import Provider, connect from 'react-redux';
import bindActionCreators from "redux";
import * as groceryActions from "./src/redux/actions/grocery-actions";
import store from './src/redux/store';

class App extends React.Component
state =
;

render()
return (
<Provider store=store>
<Layout />
</Provider>
);



export default (App);









share|improve this question

















  • 2




    It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
    – zavjs
    Nov 11 at 20:45











  • ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
    – awebdev
    Nov 11 at 21:08










  • Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
    – zavjs
    Nov 12 at 15:04














up vote
0
down vote

favorite












I'm trying to access my Redux groceries array from my grocery list component, and when I try to access the state with this.props.groceries, it returns 'undefined'. I'm still trying to wrap my head around some Redux concepts, but I think I'm really close. In my store.js, I'm logging



store.subscribe(() => 
console.log('store changed', store.getState());
);


And getState() is displaying my correct groceries array, with all the groceries inside it. I'm just not sure how to access this state from my groceries list component. Thanks!



Overview of my GroceryList component:



 import connect from "react-redux";
import bindActionCreators, createStore from 'redux';
import * as groceryActions from '../../redux/actions/grocery-actions';

class GroceryList extends Component
constructor(props)
super(props);
this.state =
;

addGroceryToList()
this.props.addGrocery(newGroceryItem);
console.log(this.props.groceries); //Logs undefined

render()
return(
//something
)



const mapStateToProps = (state) => (
groceries: state.groceries.groceries
);

const mapDispatchToProps = dispatch =>
bindActionCreators(
addGrocery: groceryActions.addGrocery, ,
dispatch
)

export default connect(mapStateToProps, mapDispatchToProps)(GroceryList);


Grocery action:



let groceryIndex = 0;

export const addGrocery = grocery => dispatch =>
dispatch(
type: 'ADD_GROCERY',
id: groceryIndex++,
grocery
);
;


Grocery reducer:



export const groceries = (state = , action) => 
switch (action.type)
case "ADD_GROCERY":
return [
...state,
grocery(action.grocery, action),
];
default:
return state



export const grocery = (state, action) =>
switch (action.type)
case "ADD_GROCERY":
return
id: action.id,
grocery: action.grocery,
;
default:
return state




Reducer combiner:



import combineReducers from 'redux';

import groceries from './grocery-reducer';

const reducer = combineReducers(
groceries: groceries,
);

export default reducer;


Store



import createStore, applyMiddleware, compose from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducers';

if (typeof window === 'undefined')
global.window =


const enhancer = compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__
? window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f
);

/* eslint-disable no-underscore-dangle */
const store = createStore(
reducer,
, // initial state
enhancer
)

store.subscribe(() =>
console.log('store changed', store.getState());
);

/* eslint-enable */

export default store


App.js



import Provider, connect from 'react-redux';
import bindActionCreators from "redux";
import * as groceryActions from "./src/redux/actions/grocery-actions";
import store from './src/redux/store';

class App extends React.Component
state =
;

render()
return (
<Provider store=store>
<Layout />
</Provider>
);



export default (App);









share|improve this question

















  • 2




    It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
    – zavjs
    Nov 11 at 20:45











  • ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
    – awebdev
    Nov 11 at 21:08










  • Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
    – zavjs
    Nov 12 at 15:04












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to access my Redux groceries array from my grocery list component, and when I try to access the state with this.props.groceries, it returns 'undefined'. I'm still trying to wrap my head around some Redux concepts, but I think I'm really close. In my store.js, I'm logging



store.subscribe(() => 
console.log('store changed', store.getState());
);


And getState() is displaying my correct groceries array, with all the groceries inside it. I'm just not sure how to access this state from my groceries list component. Thanks!



Overview of my GroceryList component:



 import connect from "react-redux";
import bindActionCreators, createStore from 'redux';
import * as groceryActions from '../../redux/actions/grocery-actions';

class GroceryList extends Component
constructor(props)
super(props);
this.state =
;

addGroceryToList()
this.props.addGrocery(newGroceryItem);
console.log(this.props.groceries); //Logs undefined

render()
return(
//something
)



const mapStateToProps = (state) => (
groceries: state.groceries.groceries
);

const mapDispatchToProps = dispatch =>
bindActionCreators(
addGrocery: groceryActions.addGrocery, ,
dispatch
)

export default connect(mapStateToProps, mapDispatchToProps)(GroceryList);


Grocery action:



let groceryIndex = 0;

export const addGrocery = grocery => dispatch =>
dispatch(
type: 'ADD_GROCERY',
id: groceryIndex++,
grocery
);
;


Grocery reducer:



export const groceries = (state = , action) => 
switch (action.type)
case "ADD_GROCERY":
return [
...state,
grocery(action.grocery, action),
];
default:
return state



export const grocery = (state, action) =>
switch (action.type)
case "ADD_GROCERY":
return
id: action.id,
grocery: action.grocery,
;
default:
return state




Reducer combiner:



import combineReducers from 'redux';

import groceries from './grocery-reducer';

const reducer = combineReducers(
groceries: groceries,
);

export default reducer;


Store



import createStore, applyMiddleware, compose from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducers';

if (typeof window === 'undefined')
global.window =


const enhancer = compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__
? window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f
);

/* eslint-disable no-underscore-dangle */
const store = createStore(
reducer,
, // initial state
enhancer
)

store.subscribe(() =>
console.log('store changed', store.getState());
);

/* eslint-enable */

export default store


App.js



import Provider, connect from 'react-redux';
import bindActionCreators from "redux";
import * as groceryActions from "./src/redux/actions/grocery-actions";
import store from './src/redux/store';

class App extends React.Component
state =
;

render()
return (
<Provider store=store>
<Layout />
</Provider>
);



export default (App);









share|improve this question













I'm trying to access my Redux groceries array from my grocery list component, and when I try to access the state with this.props.groceries, it returns 'undefined'. I'm still trying to wrap my head around some Redux concepts, but I think I'm really close. In my store.js, I'm logging



store.subscribe(() => 
console.log('store changed', store.getState());
);


And getState() is displaying my correct groceries array, with all the groceries inside it. I'm just not sure how to access this state from my groceries list component. Thanks!



Overview of my GroceryList component:



 import connect from "react-redux";
import bindActionCreators, createStore from 'redux';
import * as groceryActions from '../../redux/actions/grocery-actions';

class GroceryList extends Component
constructor(props)
super(props);
this.state =
;

addGroceryToList()
this.props.addGrocery(newGroceryItem);
console.log(this.props.groceries); //Logs undefined

render()
return(
//something
)



const mapStateToProps = (state) => (
groceries: state.groceries.groceries
);

const mapDispatchToProps = dispatch =>
bindActionCreators(
addGrocery: groceryActions.addGrocery, ,
dispatch
)

export default connect(mapStateToProps, mapDispatchToProps)(GroceryList);


Grocery action:



let groceryIndex = 0;

export const addGrocery = grocery => dispatch =>
dispatch(
type: 'ADD_GROCERY',
id: groceryIndex++,
grocery
);
;


Grocery reducer:



export const groceries = (state = , action) => 
switch (action.type)
case "ADD_GROCERY":
return [
...state,
grocery(action.grocery, action),
];
default:
return state



export const grocery = (state, action) =>
switch (action.type)
case "ADD_GROCERY":
return
id: action.id,
grocery: action.grocery,
;
default:
return state




Reducer combiner:



import combineReducers from 'redux';

import groceries from './grocery-reducer';

const reducer = combineReducers(
groceries: groceries,
);

export default reducer;


Store



import createStore, applyMiddleware, compose from 'redux';
import thunk from 'redux-thunk';

import reducer from './reducers';

if (typeof window === 'undefined')
global.window =


const enhancer = compose(
applyMiddleware(thunk),
window.__REDUX_DEVTOOLS_EXTENSION__
? window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f
);

/* eslint-disable no-underscore-dangle */
const store = createStore(
reducer,
, // initial state
enhancer
)

store.subscribe(() =>
console.log('store changed', store.getState());
);

/* eslint-enable */

export default store


App.js



import Provider, connect from 'react-redux';
import bindActionCreators from "redux";
import * as groceryActions from "./src/redux/actions/grocery-actions";
import store from './src/redux/store';

class App extends React.Component
state =
;

render()
return (
<Provider store=store>
<Layout />
</Provider>
);



export default (App);






javascript reactjs redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 20:03









awebdev

659




659







  • 2




    It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
    – zavjs
    Nov 11 at 20:45











  • ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
    – awebdev
    Nov 11 at 21:08










  • Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
    – zavjs
    Nov 12 at 15:04












  • 2




    It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
    – zavjs
    Nov 11 at 20:45











  • ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
    – awebdev
    Nov 11 at 21:08










  • Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
    – zavjs
    Nov 12 at 15:04







2




2




It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
– zavjs
Nov 11 at 20:45





It's likely your mapsStateToProps is making an incorrect reference within your GroceryList component file. const mapStateToProps = (state) => ( groceries: state.groceries.groceries ); Shouldn't it be state.groceries only?
– zavjs
Nov 11 at 20:45













ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
– awebdev
Nov 11 at 21:08




ahhhhh thanks for catching that hahahah. Works with state.groceries. @zavjs
– awebdev
Nov 11 at 21:08












Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
– zavjs
Nov 12 at 15:04




Hah! That happens all the time to me. It's always good to have an external eye to catch these things. Glad I could help
– zavjs
Nov 12 at 15:04

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252702%2funable-to-access-redux-array-state-from-component%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252702%2funable-to-access-redux-array-state-from-component%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands