score:9

Accepted answer

i modified your code to what i think will work. i also left comments.

class app extends component {

  state = {
    posts: []
  }

  componentdidmount() {
    // no need to use dispatch again. your action creators are already bound by 
    // mapdispatchtoprops.  notice also that they come from props
    const { selectedcategory, fetchcategoriesifneeded, fetchpostsifneeded} = this.props;
    fetchcategoriesifneeded(selectedcategory);
    fetchpostsifneeded(selectedcategory);
  }

  //... the same
}

function mapstatetoprops ( state ) {
  //... the same
}

function mapdispatchtoprops (dispatch) {
  // when arguments match, you can pass configuration object, which will 
  // wrap your actions creators with dispatch automatically.
  return {
    orderpost,
    fetchcategoriesifneeded,
    fetchpostsifneeded,
  }
}

score:1

you just don't. the mapdispatchtoprops does exactly what you are trying to do in your component. instead of calling a dispatch you call the method that was provided to your component by connect. in your case:

componentdidmount() {
    const { selectedcategory, fetchcategories, fetchposts} = this.props;
    fetchcategories(selectedcategory);
    fetchposts(selectedcategory);
}

score:5

in map to dispatch you have fetchcategories/fetchposts so therefore you need to call them like this:

componentdidmount() {

            const { dispatch, selectedcategory, fetchcategories, fetchposts } = this.props
            //call like this instead of fetchcategoriesifneeded/fetchpostsifneeded
            //dispatch(fetchcategories(selectedcategory))
            //dispatch(fetchposts(selectedcategory))
        }

you have this:

function mapdispatchtoprops (dispatch) {
  return {
    changeorder: (data) => dispatch(orderpost(data)),
    fetchcategories: (data) => dispatch(fetchcategoriesifneeded(data)),
    fetchposts: (data) => dispatch(fetchpostsifneeded(data))
  }
}

so you need to call fetchcategories/fetchposts from your props instead of fetchcatifneeded/fetchpostsifneeded


Related Query

More Query from same tag