react-reduxmapstatetoprops

mapDispatchToProps not updating store


I'm working on a personal project with redux. My mapStateToProps function seems to me properly written. but when I try to use it to send an object to my store nothing works.

Here's my function:

const mapDispatchToProps = dispatch => {
    return {
        addOrder: (item) => {
            dispatch(addOrder(item));
        }
    }
}

<div className="recordOrder">
                   <button onclick={() => this.props.addOrder(this.state)}>Enregistrer et lancer la commande</button>
                   </div>

And my reducer:

const initialState = {
    orderList : []
}

console.log(initialState);

export default function rootReducer ( state= initialState, action){
    const orderList = [...state.orderList];
    let position
    switch (action.type){
        case ADD_ORDER:
            return {
                orderList : [...state.orderList, action.payload]
            };
        case DELETE_ORDER:
            position = orderList.indexOf(action.payload)
                orderList.splice(position, 1)
            return {
              orderList  
            }
        default:
            return state;
    }
    console.log(state)
}

My entire component as requested:

import React, { Component } from 'react';
import { NavItem } from 'react-bootstrap';
import menu from './menu';
import { connect } from 'react-redux';

import { addOrder} from '../action'

class getOrder extends Component {
    state = {
        number: `CMD-${Date.now()}`,
        order:[],
        total: 0 ,
        menu:menu,
        isPaid: false
      }

    

    

    addItem = (index) => {
    const order = [...this.state.order];
    const menu = [...this.state.menu];
    let total = this.state.total;
    const pizza = menu[index];
    console.log(pizza);
    let ind = order.findIndex((item) => 
       item.article == pizza.name
    
    )
    if (ind === -1){
         order.push({article: pizza.name, price: pizza.price, volume:1})
        
         total = total + order[order.length-1].price 
    } else if (ind != -1){
        order[ind].volume++
        total = total + order[ind].price
    }
    
    
    


    this.setState({
        order:order,
        total:total
        
    })
    console.log("youpiii");
    console.log(this.state.total);
    console.log(this.state.order);

    }

    
    render() { 
         

        const menuDisplay= menu.map( (item) => {
            return (
            <div>
                <img onClick={() => this.addItem(item.number)} src={`${process.env.PUBLIC_URL}${item.picture}`} alt="picture" />
                <div className="tagPrice">
                <p>{item.name}</p>
                <p>{item.price} €</p>
                </div>
            </div>
            
            )
            
        });

        const currentOrder = [...this.state.order]

        const orderDisplay = currentOrder.map((item) => {
           
            
            let price = item.price*item.volume;
           console.log(price);
        
            return (
                <div>
                    <h1>{item.volume} × {item.article}</h1>
                    <p>{price} €</p>
                </div>
            )
            
        } );

        return ( 
            <div className="takeOrder">
            <div className="orderban">
            <h1>Pizza Reflex</h1>
          </div>
          
          <div>
              <div className="menuDisplay">
                  {menuDisplay}
              </div>
              <div className="orderBoard">
               <h1>Détail de la commande N°{this.state.number}</h1>
               {orderDisplay}
               <div className="total">
                   <h2>Soit un total de {this.state.total} € </h2>
               </div>
               <div className="recordOrder">
                   <button onclick={() => this.props.addOrder(this.state)}>Enregistrer et lancer la commande</button>
                   </div>
              </div>
          </div>
          </div>

         );
    }
}


 const mapDispatchToProps = dispatch => {
    return {
        addOrder: (item) => {
            dispatch(addOrder(item));
        }
    }
}  
 
export default connect ( mapDispatchToProps) (getOrder);


Can someone tell me what I've missed ?

Thanks for your help !


Solution

  • What you are missing is more of your code it can not be solved with what you have.

    In more details what I need is the this.state , combinedReducer

    The easiest fix you can do now is changing yow mapDispatchToProps works better if it is an obj

    
     const mapStateToProps = (state) => {
          return {
           // here you specified the properties you want to pass yow component fom the state
          }
      };
     const mapDispatchToProps = {action1, action2};  
     
    export default connect ( mapDispatchToProps) (getOrder);
    
    
    

    connectreceives two params mapStateToProps and mapDispatchToProps,

    mapDispatchToProps is optional, but mapStateToProps is mandatory, there for you need to specified, if your are not going to pass anything you need to pass a null value

    export default connect (null, mapDispatchToProps) (getOrder);

    also avoid exporting components without a name example

    function MyButton () {}
    const MyButtonConnect = connect(state, dispatch)(MyButton);
    export default MyButtonConnect