score:0
if you want to get your state value in the reducer you don't need getstate(). the state value is the first argument in your reducer. so you just call the state in your reducer. if you want to get the state value in your action then you can use getstate. also if you want to extract blogs object from the state object you are going one too far.
your are doing this:
const { blogs } = store.getstate().blogs;
and it should be this:
const { blogs } = store.getstate();
here is an example.
reducer:
const initialstate = {
//your initial state
}
export default (state = initialstate, action) => {
switch(action.type){
case "update_user":
// just use the state object
const { blogs } = state;
const username = action.data.id.username;
const usertochange = blogs.find(a => a.username === username);
const blogsneedtochange = usertochange.blogs
const changeduserblogs = ?????????????
return state.map(user =>
user.username !== username ? user : changeduserblogs
);
}
}
action:
export const updateuser = (id, blog) => {
return async (dispatch, getstate) => {
const { blogs } = getstate();
dispatch({
type: "update_user",
data: {
id: id,
data: blog,
}
});
};
};
score:1
there is a faq part in the redux documentation about this issue: https://redux.js.org/faq/reducers#how-do-i-share-state-between-two-reducers-do-i-have-to-use-combinereducers
you could use reduce-reducers for example:
// in this reducer, the state argument represents your root state
// here you can handle actions which need to access multiple slice of the root state
const specializedreducer = (state, action) => {
switch (action.type) {
case "update_user":
const { blogs, users } = state;
// proceed with your code here
}
}
const reducer = combinereducers({
blogs: blogreducer,
notification: notificationreducer,
search: searchreducer,
filter: filterreducer,
users: userreducer,
loggeduser: loginreducer
});
const rootreducer = reducereducers(specializedreducer, reducer);
const store = createstore(rootreducer, composewithdevtools(applymiddleware(thunk)));
score:1
so, as suspected, i was overly complicating things. thanks to the back and forth here, i realized that i could call another listener from another reducer. thus this is how i solve my issue.
in my user reducer i added a case "user_posted_blog"
case "user_posted_blog":
return [...action.data];
and then in my blog reducer i added an additional dispatch to that newly created case, which would grab all of the users and update the state after the new blog is created
export const createblog = content => {
return async dispatch => {
const newblog = await blogservice.create(content);
dispatch({
type: "new_blog",
data: newblog
});
const updatedusers = await userservice.getall();
dispatch({
type: "user_posted_blog",
data: updatedusers
});
};
};
Source: stackoverflow.com
Related Query
- Accessing Store's State in Redux From Within A Reducer Function
- Accessing a part of reducer state from one reducer within another reducer
- Accessing a reducer state from within another reducer
- delete song from redux state within reducer
- React hooks: accessing up-to-date state from within a callback
- React get state from Redux store within useEffect
- Redux Toolkit: state showing as Proxy / undefined within reducer
- Is it possible to call a reducer function from another reducer function (within the same slice) using Redux Toolkit?
- Accessing the state from within a redux-observable epic
- Delete element from array in redux state using a reducer in createSlice
- React-Redux: Copying an array from the state in Reducer function so that you can modify it
- Pass a state value from redux reducer to another reducer
- State is unchanged from within a function returned by a hook
- Redux toolkit accessing state from thunk
- how to Modify a child object from you state in the reducer with Redux
- Accessing latest state value from different function
- React- Unable to change state from child component using function defined within a container component
- Unexpected state changes in Redux Reducer function
- Redux accessing state from action
- React Redux Redux Form - moving a function from within a <Field> produces error
- Redux store can not get state from reducer
- Updating an object within array in react redux reducer getting map() is not a function
- Pass state from one reducer to another in Redux
- How can I dispatch a state change to redux when calling it from a function inside the component?
- measure state update from within event handler function
- Event handler not accessing the correct value from redux state
- React Redux - Calling a function from within a simple action cause "not a function" error
- I get undefined while trying to access state from root reducer with redux toolkit
- How do I access the Context and Component state from within a DOM callback in a function React component?
- How to only change selected state value from useDispatch function in redux
More Query from same tag
- How can I get the email id of the clicked span element which is hidden property in css
- React Redux with Typescript, Property Not Available in Reducer from ActionType
- How to use HOC to assign a value to a component in react?
- JSX element type 'X' is not a constructor function for JSX elements. types returned by 'render()' are incompatible between these types
- React Memo Renders Every Time with same given props
- Split mixed bills in react
- how to render a specific field dynamically in Material Table React
- React Context Provider not passing down state into children
- Can't type in input field?
- Can successfully open modal when I need to but cannot close it
- Generalizing function to reuse with different elements
- How to prevent rerendring page when a condition is not verified with redux
- Calling Child component function from Parent component in React.js
- Possible for input/useState not to be set on button click?
- I can't understand why and how are we passing something to this function. Selector question
- Customize React Router Render Order
- React Router v5 accompanied with Code Splitting, and Data Prefetching with the use of Server Side Rendering
- React.js No-op in component?
- What is the difference between passing a function as a prop with or without parentheses in React?
- TypeScript - Argument of type 'String' is not assignable to parameter of type 'string'
- Gatsby window not available during server side rendering build error
- Configuring next.config file
- How do I delay an animation in gsap for react
- Drawer rendering inside AppBar element
- TypeError: this.state.employees.map is not a function
- calling setState in an arrow function not altering the state?
- could not render component in react
- React Native, there is no route defined for index undefined
- What is the appropriate lifecycle stage to do redirects in a React component?
- How to make item list auto update rendered list when item is deleted?