JavaScript Problems accessing Connect function value in another Component in a React-Redux App

To access data you need to pass the state to the connect api. You only passed in the dispatch action to the props

function mapStateToProps(state) {
  return {
    SomeDataProperty: state.Reducer
  }
}

Also your mapDispatchToProps is in the return statement of your component, it is better practice to put that function in a different file (the container) and then point that container with the connected props to the WebShop component if you are trying to make WebShop pass props to the Cart component.

Example:

WebshopContainer.jsx (container component, which will pass the props to the component)

import WebShop from './WebShop';

function mapStateToProps(state) {
  return {
    SomeDataProperty: state.Reducer // prop Reducer was taken from your reducer.js because it was passed into here from the connect API
  }
}

function mapDispatchToProps (dispatch) {
    return {
        onCartAdd: (cart) => {
            dispatch(addCart(cart));
        },
    }
}

export default connect(mapStateToProps, mapDispatchToProps)( WebShop ); // pass in the component here 

component file that has component WebShop and you want to pass props to component so that component can access the props:

class WebShop extends Component {
  constructor(props) {
    super(props);
    this.state = {
      cartData: {}
    }
  }
  componentWillReceiveProps() {
    const waitForProps = setInterval(() => {
      if (this.props.Reducer.hasOwnProperty('some prop you gave it')) {
        clearInterval(waitForProps);
        this.setState({ cartData: this.props.Reducer['some data property you used'] });
      }
    });
  }
  handleClick() {
      let cart = {price:0,item:"userselection",size:this.state.value};
      this.props.onCartAdd(cart);
  }
  render() {
    return (
      <div>
        <Cart cart={ this.state.cartData }
      </div>
    );
  }
}
Answer:1

I saw a couple of issues here:

  1. You are not importing the action when you create your connected component

    import { addCart } from './actions'
    // ... rest of your code: component, mapStateToProps, etc.
    const mapDispatchToProps = (dispatch) => {
      return {
        onCartAdd: (cart) => {
          dispatch(addCart(cart));
        }
      }
    }
    
  2. Your reducer MUST return a default state

    function Reducer(state = {}, action){
      switch(action.type){
        case ADD_CART:
          return {
              ...state,
              cart: action.payload
          }
    
        default: 
           return state          
    
      }
    }
    

EDIT

Another bug I found. You are returning the name of the reducer only. Should you not also return cart?

function mapStateToProps (state) {
  return {
    cart: state.reducer.cart
  }
}
Answer:2

I have a table inside modal that has several hundreds items. Each item row is associated with particular bag. All bags are listed on a page and when user clicks "Add Items" modal opens up and shows ...

I have a table inside modal that has several hundreds items. Each item row is associated with particular bag. All bags are listed on a page and when user clicks "Add Items" modal opens up and shows ...

I have been working with Force-Directed Graph but I am still new to D3js and Javascript. I want to able to click on the node and the infobox popup on the page and print some information about that ...

I have been working with Force-Directed Graph but I am still new to D3js and Javascript. I want to able to click on the node and the infobox popup on the page and print some information about that ...

  1. d3js force directed graph
  2. d3js force directed
  3. d3js force directed graph labels
  4. d3js force directed graph zoom
  5. d3js force directed graph example
  6. d3js force directed graph arrows
  7. d3.js force directed graph tutorial
  8. d3.js force directed graph group
  9. d3.js force directed node focus
  10. d3.js force directed graph tooltip
  11. d3.js force directed graph filter
  12. d3.js force directed graph performance
  13. d3.js force directed graph angular
  14. d3.js force directed center
  15. d3.js force directed example
  16. d3js v5 force directed graph
  17. d3js force directed layout
  18. d3.js v4 force directed graph with labels
  19. d3.js v4 force directed graph
  20. d3 js 3d force directed graph

I have this code in Javascript: var menuPosition = $('.scroll-nav').offset().top; var menuPos = 110; if (menuPosition = menuPos) { $('.scroll-nav').addClass('stick-menu'); $('.fake-nav')....

I have this code in Javascript: var menuPosition = $('.scroll-nav').offset().top; var menuPos = 110; if (menuPosition = menuPos) { $('.scroll-nav').addClass('stick-menu'); $('.fake-nav')....

I'm making a Simon game, where there are 4 quarter circles all with class name 'colorButton': red, yellow blue and green. I'd like it so that when it's the computer turn, all pointer events to the 4 ...

I'm making a Simon game, where there are 4 quarter circles all with class name 'colorButton': red, yellow blue and green. I'd like it so that when it's the computer turn, all pointer events to the 4 ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada goose
  6. cancun flights
  7. cane corso
  8. canes
  9. canadian prime minister
  10. canker sore
  11. candy
  12. cancer
  13. cancun
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis