Is it okay to use the same reducer to update one of two state properties depending on other state value?

So let’s say hypothetically, I have one reducer where I want to either update state.propertyA OR state.propertyB depending on the value of state.propertyC. something like: const conditionalReducer: CaseReducer<StateType, PayloadActionType> = (state, action) => { if(state.propertyC === true) { state.propertyA = action.payload; } else { state.propertyB = action.payload; } } Is this considered good practice, or… Read More Is it okay to use the same reducer to update one of two state properties depending on other state value?

Thunk Middleware: how/why does action creator have access to dispatch when it's not passed in?

For reference, this is the origin of the below screenshots: https://github.com/gopinav/React-Redux-Tutorials/blob/master/redux-demo/asyncActions.js Looking at the fetchUsers action creator function in the below screenshot, how is dispatch passed into this function? Not clear how the fetchUsers function has access to dispatch and can pass it into the anonymous return function within fetchUsers. The store creation in the… Read More Thunk Middleware: how/why does action creator have access to dispatch when it's not passed in?

Path `comment` is required. MERN stack

I don’t understand why I get this error. This is my controller: export const createProductReview = async (req, res) => { const { rating, comment } = req.body; const product = await Product.findById(req.params.id); if (product) { const alreadyReviewed = product.reviews.find( r => r.user.toString() === req.user.userId.toString() ); if (alreadyReviewed) { throw new NotFoundError(‘Product already reviewed’); }… Read More Path `comment` is required. MERN stack

How to prevent useSelector from causing unnecessary renders?

I’m using useSelector hook to retrieve the values of my reducer, but it is causing a lot of unnecessary renders on my application. It doesn’t matter which property I’m using on a component, since they are all getting the same state object from the reducer, every time one property changes, all components that use useSelector… Read More How to prevent useSelector from causing unnecessary renders?

Redux Async Thunk, API keeps responding with pending

I am creating an app about goal tracker. When I logout from an account, everything goes okay except the the logout gets stuck in pending state. There is also error in console saying Cannot read properties of null (reading ‘token’) Dashboard.jsx:20. Dashboard.jsx import React from ‘react’; import { useEffect } from ‘react’; import { useSelector,… Read More Redux Async Thunk, API keeps responding with pending

Can router.push be used from Redux thunk? Is it a good practice?

I have this anchor element: <a className="btn btn-sm btn-circle" href={`https://www.facebook.com/sharer/sharer.php?u=${ process.env.NEXT_PUBLIC_ENVIRONMENT == "prod" ? "https://tikex.com&quot; : "https://tikex-staging.com&quot; }/share/${organizationSlug}/${postId}&quote=${postSurveySchemaDTO?.caption}`} onClick={(e) => { dispatch( createShare({ tempUserId: "e3445c3b-5513-4ede-8229-d258ed4418ae", postId, }) ); }} > Megosztom </a> I tested many times, maybe one time only but seem than onclick and networking was not triggered in that case, 99% ok. Is… Read More Can router.push be used from Redux thunk? Is it a good practice?

Where to put useDispatch and bindActionCreators in a project?

My question is this, where do I put the methods mentioned above? Because in each component in which I want to use the Redux store, I need to basically repeat the mantra of, import { useSelector, useDispatch } from "react-redux"; import { bindActionCreators } from "redux"; import * as actions from "../../redux/actions"; and then, for… Read More Where to put useDispatch and bindActionCreators in a project?