score:9
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
Source: stackoverflow.com
Related Query
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- How to pass Dispatch to action when using redux form
- How to dispatch action when a state changes in redux toolkit without using useEffect?
- When using React / Redux, how is dispatching an action that merely changes the Redux state vs actually doing some task?
- react / redux - How to dispatch an action on componentDidMount
- How to manage submission errors on react final form when using a redux action on onSubmit?
- How to stub async action in componentDidMount for react redux connected component test using Jest
- How to dispatch Redux action from stateless component when route is loaded?
- How to dispatch an action when clicking on Link when using React-Router & Redux?
- Redux action return type when using dispatch and getState
- React Redux how to dispatch using ownProps route parameter
- How to fix blank screen when using react router in redux
- How to dispatch an action when another synchronous action is complete in Redux
- Correct way to write in dependency array when dispatch an action using hooks in redux
- How and where to dispatch action when react router changes props
- How to dispatch an action when another becomes fulfilled with reduxjs/toolkit React and Typescript?
- How to dispatch an action just if another action was correctly dispatched ? Using redux thunk (react)
- How can I change the value of an object in an array in react redux when the action is called?
- How to dispatch an action when component did mount using Hooks in a connected function component?
- How to get error message in terminal when using Redux React
- how to dispatch same action with different params in react redux
- How to dispatch action in event handler in React & Redux application
- How to call action in button click of component, Even though i'm using connect function which is not calling in React Redux
- How to redirect to a different page after a redux action using react
- How I should dispatch the action to fetch the data using react components and redux?
- How Should I Dispatch Actions When Using React Hooks?
- Can't figure out how to pass props to components when using React Router v5 and Redux
- Why my action result data is null when I try console the state using redux and react js
- React Native: How to dispatch redux action from Custom Drawer
- How to dispatch async action in Redux using Redux Thunk: TypeError: Cannot read property 'loading' of undefined
More Query from same tag
- Moving cursor in react slate-editor
- Updating object's state in redux
- Typescript how to pass props in parent component
- Using React.js props on Redux's 'compose'
- Material UI: drawer with expandable side menu
- after creating a react app npm start doesnt work. No error message given
- How can I change ID of copied element
- How to pass text box value to sibling component in react on button click
- How to show dummy image in React when url is broken?
- How to display component when page load on reactjs
- How to get Emotion styled component in a React project to work with TypeScript?
- React.JS simple component not rendering nested html tag
- How to Hide Navbar items on URL Change?
- Updating a single item in mongoDB whilst keeping the rest the same
- component dont rerender when I remove item from state but it rerender when I add item
- React Router 4 relative paths
- Can styled components handle computations like SCSS?
- Trigger a function in another component on click a button from a different component in React
- Is there any way to know the useParams from the top container?
- react-router redirect render nothing in protected route
- how to make a message without wrapping in span in react-intl
- react helper import fails in production but works in development
- Why does my update in a put request overwrite the whole record in Sequelize?
- Returning new store in Redux
- How to bootstrap component using react-router
- ReactJS dont take the css styles
- Trying to implement a nested If Else statement inside REACTJS. Get error 0 is not a function
- redux-react-route v4/v5 push() is not working inside thunk action
- React Error: NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node
- Is React.lazy gonna improve React Native performance?