Redux- Not getting state from mapStateToProps












0















i am new to redux i have created a code and while accessing connect method for state in store, i am getting error and undefined.code is given below.



action:



export const ADD_INGREDIENT = 'ADD_INGREDIENT';
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT';


reducer:



import * as actionTypes from './actions';

const initialState={
ingredients: {
salad:0,
meat:0,
cheese:0,
},
totalprice:40
}

const reducer = (state=initialState, action)=>{
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]+1
},
};
case actionTypes.REMOVE_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]-1,
},
};
default:
return state;
}
};
export default reducer;


my component:



import React, {Component} from 'react';
import Burger from "../../components/burger/burger";
import Aux from '../../hoc/aux';
import classes from './BurgerBuilder.css';
import BuildControls from '../../components/burger/BuildControls/BuildControls';
import Totalprice from '../../components/burger/TotalPrice/TotalPrice';
import * as actionTypes from '../../store/actions';
import { connect } from 'react-redux';
import {store} from '../../App';

const ingredientCost = {
cheese:20,
meat:30,
salad:10,
};

class BurgerBuilder extends Component{
constructor(props){
super(props);
this.state={
initialValue: store.getState().ingredients,
totalPrice:0,
}
}

render(){
const disabledInfo ={
...this.props.ings
}
if(this.props.ings){
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger ingredients={this.props.ings}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}

else{
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger
ingredients={this.state.initialValue}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}
}
}

const mapStateToProps = state =>{
return console.log(state);
}
const mapDispatchToProps = dispatch =>{
return{
newIngredientAdd: (ingName)=>dispatch({type:actionTypes.ADD_INGREDIENT,ingredientName:ingName}),
newIngredientRemove: (ingName)=>dispatch({type:actionTypes.REMOVE_INGREDIENT,ingredientName:ingName})
}
}

export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


app.js



import React, { Component } from 'react';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import reducer from './store/reducer';
import {createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

export const store = createStore(reducer, applyMiddleware(thunk))
class App extends Component {
render() {
return (
<Provider store={store}>
<BrowserRouter>
<BurgerBuilder/>
</BrowserRouter>
</Provider>
);
}
}

export default App;


this is the code which i have and i wrapped app.js with provider and while consoling inside the reducer i am able to get the output but while in mycomponent i am not getting the proper output.



Thanks in advance.










share|improve this question























  • what error are you getting? can you share error details/stack trace?

    – Syed Osama Maruf
    Nov 19 '18 at 9:05











  • while consoling state i am getting like this

    – rnr
    Nov 19 '18 at 9:07











  • ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

    – rnr
    Nov 19 '18 at 9:07











  • can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

    – Syed Osama Maruf
    Nov 19 '18 at 9:10
















0















i am new to redux i have created a code and while accessing connect method for state in store, i am getting error and undefined.code is given below.



action:



export const ADD_INGREDIENT = 'ADD_INGREDIENT';
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT';


reducer:



import * as actionTypes from './actions';

const initialState={
ingredients: {
salad:0,
meat:0,
cheese:0,
},
totalprice:40
}

const reducer = (state=initialState, action)=>{
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]+1
},
};
case actionTypes.REMOVE_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]-1,
},
};
default:
return state;
}
};
export default reducer;


my component:



import React, {Component} from 'react';
import Burger from "../../components/burger/burger";
import Aux from '../../hoc/aux';
import classes from './BurgerBuilder.css';
import BuildControls from '../../components/burger/BuildControls/BuildControls';
import Totalprice from '../../components/burger/TotalPrice/TotalPrice';
import * as actionTypes from '../../store/actions';
import { connect } from 'react-redux';
import {store} from '../../App';

const ingredientCost = {
cheese:20,
meat:30,
salad:10,
};

class BurgerBuilder extends Component{
constructor(props){
super(props);
this.state={
initialValue: store.getState().ingredients,
totalPrice:0,
}
}

render(){
const disabledInfo ={
...this.props.ings
}
if(this.props.ings){
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger ingredients={this.props.ings}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}

else{
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger
ingredients={this.state.initialValue}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}
}
}

const mapStateToProps = state =>{
return console.log(state);
}
const mapDispatchToProps = dispatch =>{
return{
newIngredientAdd: (ingName)=>dispatch({type:actionTypes.ADD_INGREDIENT,ingredientName:ingName}),
newIngredientRemove: (ingName)=>dispatch({type:actionTypes.REMOVE_INGREDIENT,ingredientName:ingName})
}
}

export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


app.js



import React, { Component } from 'react';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import reducer from './store/reducer';
import {createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

export const store = createStore(reducer, applyMiddleware(thunk))
class App extends Component {
render() {
return (
<Provider store={store}>
<BrowserRouter>
<BurgerBuilder/>
</BrowserRouter>
</Provider>
);
}
}

export default App;


this is the code which i have and i wrapped app.js with provider and while consoling inside the reducer i am able to get the output but while in mycomponent i am not getting the proper output.



Thanks in advance.










share|improve this question























  • what error are you getting? can you share error details/stack trace?

    – Syed Osama Maruf
    Nov 19 '18 at 9:05











  • while consoling state i am getting like this

    – rnr
    Nov 19 '18 at 9:07











  • ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

    – rnr
    Nov 19 '18 at 9:07











  • can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

    – Syed Osama Maruf
    Nov 19 '18 at 9:10














0












0








0








i am new to redux i have created a code and while accessing connect method for state in store, i am getting error and undefined.code is given below.



action:



export const ADD_INGREDIENT = 'ADD_INGREDIENT';
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT';


reducer:



import * as actionTypes from './actions';

const initialState={
ingredients: {
salad:0,
meat:0,
cheese:0,
},
totalprice:40
}

const reducer = (state=initialState, action)=>{
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]+1
},
};
case actionTypes.REMOVE_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]-1,
},
};
default:
return state;
}
};
export default reducer;


my component:



import React, {Component} from 'react';
import Burger from "../../components/burger/burger";
import Aux from '../../hoc/aux';
import classes from './BurgerBuilder.css';
import BuildControls from '../../components/burger/BuildControls/BuildControls';
import Totalprice from '../../components/burger/TotalPrice/TotalPrice';
import * as actionTypes from '../../store/actions';
import { connect } from 'react-redux';
import {store} from '../../App';

const ingredientCost = {
cheese:20,
meat:30,
salad:10,
};

class BurgerBuilder extends Component{
constructor(props){
super(props);
this.state={
initialValue: store.getState().ingredients,
totalPrice:0,
}
}

render(){
const disabledInfo ={
...this.props.ings
}
if(this.props.ings){
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger ingredients={this.props.ings}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}

else{
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger
ingredients={this.state.initialValue}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}
}
}

const mapStateToProps = state =>{
return console.log(state);
}
const mapDispatchToProps = dispatch =>{
return{
newIngredientAdd: (ingName)=>dispatch({type:actionTypes.ADD_INGREDIENT,ingredientName:ingName}),
newIngredientRemove: (ingName)=>dispatch({type:actionTypes.REMOVE_INGREDIENT,ingredientName:ingName})
}
}

export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


app.js



import React, { Component } from 'react';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import reducer from './store/reducer';
import {createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

export const store = createStore(reducer, applyMiddleware(thunk))
class App extends Component {
render() {
return (
<Provider store={store}>
<BrowserRouter>
<BurgerBuilder/>
</BrowserRouter>
</Provider>
);
}
}

export default App;


this is the code which i have and i wrapped app.js with provider and while consoling inside the reducer i am able to get the output but while in mycomponent i am not getting the proper output.



Thanks in advance.










share|improve this question














i am new to redux i have created a code and while accessing connect method for state in store, i am getting error and undefined.code is given below.



action:



export const ADD_INGREDIENT = 'ADD_INGREDIENT';
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT';


reducer:



import * as actionTypes from './actions';

const initialState={
ingredients: {
salad:0,
meat:0,
cheese:0,
},
totalprice:40
}

const reducer = (state=initialState, action)=>{
switch (action.type) {
case actionTypes.ADD_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]+1
},
};
case actionTypes.REMOVE_INGREDIENT:
return {
...state,
ingredients:{
...state.ingredients,
[action.ingredientName] : state.ingredients[action.ingredientName]-1,
},
};
default:
return state;
}
};
export default reducer;


my component:



import React, {Component} from 'react';
import Burger from "../../components/burger/burger";
import Aux from '../../hoc/aux';
import classes from './BurgerBuilder.css';
import BuildControls from '../../components/burger/BuildControls/BuildControls';
import Totalprice from '../../components/burger/TotalPrice/TotalPrice';
import * as actionTypes from '../../store/actions';
import { connect } from 'react-redux';
import {store} from '../../App';

const ingredientCost = {
cheese:20,
meat:30,
salad:10,
};

class BurgerBuilder extends Component{
constructor(props){
super(props);
this.state={
initialValue: store.getState().ingredients,
totalPrice:0,
}
}

render(){
const disabledInfo ={
...this.props.ings
}
if(this.props.ings){
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger ingredients={this.props.ings}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}

else{
return(
<Aux>
<h1 className={classes.Header}>Burger</h1>
<Burger
ingredients={this.state.initialValue}/>
<div>
{this.state.totalPrice !== 0 ?
(<Totalprice
totalAmount={this.state.totalPrice}
/>):(<div></div>)}
<BuildControls
increaseIngredientCount={this.props.newIngredientAdd}
decreaseIngredientCount={this.props.newIngredientRemove}
/></div>
</Aux>
)
}
}
}

const mapStateToProps = state =>{
return console.log(state);
}
const mapDispatchToProps = dispatch =>{
return{
newIngredientAdd: (ingName)=>dispatch({type:actionTypes.ADD_INGREDIENT,ingredientName:ingName}),
newIngredientRemove: (ingName)=>dispatch({type:actionTypes.REMOVE_INGREDIENT,ingredientName:ingName})
}
}

export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


app.js



import React, { Component } from 'react';
import {BrowserRouter} from 'react-router-dom';
import {Provider} from 'react-redux';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import reducer from './store/reducer';
import {createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

export const store = createStore(reducer, applyMiddleware(thunk))
class App extends Component {
render() {
return (
<Provider store={store}>
<BrowserRouter>
<BurgerBuilder/>
</BrowserRouter>
</Provider>
);
}
}

export default App;


this is the code which i have and i wrapped app.js with provider and while consoling inside the reducer i am able to get the output but while in mycomponent i am not getting the proper output.



Thanks in advance.







reactjs redux react-redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 8:52









rnrrnr

318




318













  • what error are you getting? can you share error details/stack trace?

    – Syed Osama Maruf
    Nov 19 '18 at 9:05











  • while consoling state i am getting like this

    – rnr
    Nov 19 '18 at 9:07











  • ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

    – rnr
    Nov 19 '18 at 9:07











  • can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

    – Syed Osama Maruf
    Nov 19 '18 at 9:10



















  • what error are you getting? can you share error details/stack trace?

    – Syed Osama Maruf
    Nov 19 '18 at 9:05











  • while consoling state i am getting like this

    – rnr
    Nov 19 '18 at 9:07











  • ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

    – rnr
    Nov 19 '18 at 9:07











  • can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

    – Syed Osama Maruf
    Nov 19 '18 at 9:10

















what error are you getting? can you share error details/stack trace?

– Syed Osama Maruf
Nov 19 '18 at 9:05





what error are you getting? can you share error details/stack trace?

– Syed Osama Maruf
Nov 19 '18 at 9:05













while consoling state i am getting like this

– rnr
Nov 19 '18 at 9:07





while consoling state i am getting like this

– rnr
Nov 19 '18 at 9:07













ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

– rnr
Nov 19 '18 at 9:07





ƒ (action) { if (typeof action === 'function') { return action(dispatch, getState, extraArgument); } return next(action); }

– rnr
Nov 19 '18 at 9:07













can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

– Syed Osama Maruf
Nov 19 '18 at 9:10





can you create a working example? or share minimum code to reproduce this issue? you can use codesandbox.io/s/new to create a working example...

– Syed Osama Maruf
Nov 19 '18 at 9:10












1 Answer
1






active

oldest

votes


















0














Your problem is in this line:



export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


You have reversed the order of the functions that you are passing in connect, That's why your console.log outputs the dispatch function.
Try like that:



export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder)





share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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%2f53371130%2fredux-not-getting-state-from-mapstatetoprops%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your problem is in this line:



    export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


    You have reversed the order of the functions that you are passing in connect, That's why your console.log outputs the dispatch function.
    Try like that:



    export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder)





    share|improve this answer




























      0














      Your problem is in this line:



      export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


      You have reversed the order of the functions that you are passing in connect, That's why your console.log outputs the dispatch function.
      Try like that:



      export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder)





      share|improve this answer


























        0












        0








        0







        Your problem is in this line:



        export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


        You have reversed the order of the functions that you are passing in connect, That's why your console.log outputs the dispatch function.
        Try like that:



        export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder)





        share|improve this answer













        Your problem is in this line:



        export default connect(mapDispatchToProps,mapStateToProps)(BurgerBuilder)


        You have reversed the order of the functions that you are passing in connect, That's why your console.log outputs the dispatch function.
        Try like that:



        export default connect(mapStateToProps, mapDispatchToProps)(BurgerBuilder)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 9:25









        StundjiStundji

        442212




        442212






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371130%2fredux-not-getting-state-from-mapstatetoprops%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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?