How to filter Redux actions? - reactjs

Is it okay to add more info to an action so component specific reducers (and sagas/whatever side effect lib you're using) can filter them?
Example:
function reducerComponentA(state, action) {
switch (action.type) {
case START_FETCH:
return {
...state,
isLoading: true,
};
break;
case START_FETCH_SUCCESS:
return {
...state,
isLoading: false,
};
break;
}
return state;
}
and
function reducerComponentB(state, action) {
switch (action.type) {
case START_FETCH:
return {
...state,
isLoading: true,
};
break;
case START_FETCH_SUCCESS:
return {
...state,
isLoading: false,
};
break;
}
return state;
}
Notice how both reducers observes the same action and act on them (show a loading animation). Now if the screen/component that these reducers are related to are both in memory, the START_FETCH will cause to both of them to show the loading animation, maybe even overlapping (because it's global). Is filtering actions by screen/component a good solution?
Like this:
function reducerComponentA(state, action) {
if (action.currentScreen === 'ScreenA') {
switch (action.type) {
...
}
}
return state;
}
This seems to more of a problem on React Native, because if you're using a Navigator, there's a chance multiple screens will be loaded at the same time.

You can 'mount' reducer to the different slices of the state. To achieve this, you can add path to the action, and in the reducer, update corresponding slice of the state.
It can be similar to:
function reducer(state, action) {
if (action.type === '...') {
return _.set(_.deepClone(state), `${action.path}.isLoading`, false)
} else return state;
}
In other words, action determines which part of the state reducer will be operating with.
Note that this example above is extremely inefficient and only for demo purpose. Instead of cloning the state, some immutability helpers should be used: kolodny/immutability-helper, mweststrate/immer, other.
UPD
Imagine you have action and reducer for an input state:
const UPDATE_VALUE = 'UPDATE_VALUE';
const updateValue = (value) => ({ type: UPDATE_VALUE, value })
function reducer(state, action) {
if (action.type === UPDATE_VALUE) {
return { ...state, input: action.value }
} else return state;
}
And you want to use this action/reducer for many different inputs. The action can be supplied with
a property path that indicates which part or the state should be updated, and eventually which input
will receive new props:
const UPDATE_VALUE = 'UPDATE_VALUE';
const updateValue = (value, path) => ({ type: UPDATE_VALUE, value, path })
function reducer(state, action) {
if (action.type === UPDATE_VALUE) {
return { ...state, [action.path]: action.value }
} else return state;
}
This can be used then:
dispatch(updateValue(event.target.value, 'firstNameInput'))
dispatch(updateValue('Doe', 'lastNameInput'))
The code at the beginning of the answer is a generic version of the latter.

Related

Redux reducer state was not updating with previous state while using hooks

Hi currently i am creating a project using hooks with redux.
while every time i trigger a new request ...state was not maintaining the previous data.so please help upon this .
const initialState = {
isLoaded: false,
followData:{},
FollowerCountData:{}
}
export default function followReducer(state = initialState, action) {
switch (action.type) {
case allActions.FETCH_FOLLOW_DATA:
return action;
case allActions.RECIEVE_FOLLOW_DATA:
return {
...state,
followData: action.payload,
};
case allActions.FETCH_FOLLOWER_COUNT:
return action;
case allActions.RECIEVE_FOLLOWER_COUNT:
return {
...state,
FollowerCountData: action.payload,
};
default: return state;
}
}

Two Boolean states in the Init State of Reducer, One returns undefined

I've been working with redux for the last couple weeks and was incorporating it into my projects when I ran into this wall. Pretty common reducer for modals being rendered so i can animate them before unmounting them.
const initialState = {
isModalOpen: false,
test: false
}
export default function(state = initialState, action) {
switch (action.type) {
case "modalInteraction":
return {
isModalOpen: action.payload
};
case "testModalInteraction":
return {
test: action.payload
};
default:
return state;
};
}
Sadly, the test property is still returning as undefined despite the fact that the other initial state in the same reducer can be called without a problem. I even removed all the testModalInteraction dispatches in the case that that somehow upset the datatype. I just can't spot the difference that keeps returning undefined.
When you return the new state, make sure to spread the initial state (...state) and then change whatever values you need to change.
const initialState = {
isModalOpen: false,
test: false
}
export default function(state = initialState, action) {
switch (action.type) {
case "modalInteraction":
return {
...state,
isModalOpen: action.payload
};
case "testModalInteraction":
return {
...state,
test: action.payload
};
default:
return state;
};
}
If it is still undefined, make sure the payloads are defined for both actions.
For example, your modalInteraction action could look like
export const modalInteraction = (bool) => ({
type: "modalInteraction",
payload: bool
})
P.S., you can destructure the action object. This allows you to use "type" instead of "action.type" and "payload" instead of "action.payload".
const initialState = {
isModalOpen: false,
test: false
}
export default function(state = initialState, action) {
const {type, payload} = action;
switch (type) {
case "modalInteraction":
return {
...state,
isModalOpen: payload
};
case "testModalInteraction":
return {
...state,
test: payload
};
default:
return state;
};
}

react-redux with redux thunk with Router

I have this git repo i created
https://github.com/markortiz905/emp-app
Ive been practicing reactjs and wanted to learn about redux-thunk,
at first kinda easy but I fall short on understanding how it works on routes as well.
My investigation led me to think that data fetched from server is not triggering update component due to routing ?
If anyone have time to take a look on my repo its just few files and codes simple fetch empmloyee and display on view
Heres my reducer.js snippet
const initStates = {
employees: [],
loading: true
};
function rootReducer(state = initStates, action) {
console.log(state.employees);
if (action.type == UPDATE_EMPLOYEES) {
state.employees = action.payload;
} else if (action.type == LOADING) {
state.loading = action.payload;
}
//means something happen bad
return state;
}
I just found out whats wrong, it seems that I am doing it wrong from the very start in my reducer script
This is wrong, I am updating employees from the const variable but const cant be updated right? once you’ve assigned a value to a variable using const, you can’t reassign it to a new value. source - https://tylermcginnis.com/var-let-const/
const initStates = {
employees: [],
loading: true
};
function rootReducer(state = initStates, action) {
console.log(state.employees);
if (action.type == UPDATE_EMPLOYEES) {
state.employees = action.payload;
} else if (action.type == LOADING) {
state.loading = action.payload;
}
//means something happen bad
return state;
}
I changed my reducer to return the new object instead.
function rootReducer(state = initStates, action) {
switch (action.type) {
case UPDATE_EMPLOYEES_STARTED:
return {
...state,
loading: true,
employees: null,
};
case UPDATE_EMPLOYEES:
return {
...state,
loading: false,
error: null,
employees: action.payload,
};
case UPDATE_EMPLOYEES_ENDED:
return {
...state,
loading: false,
employees: [...state.employees],
};
default:
return state;
}
}

Reducer always returning initial state

I have been playing with react-native and redux and I have encountered an error. The state is always initialised as the starting one although the payload is present when I debug it inside the reducer.
This is my reducer file
let initialState = {
filterList: [],
isFetching: false,
activeFilters: [],
}
export function fetchFilterList(state = initialState, action) {
return { ...state, isFetching: true };
}
export function fetchFilterListSuccess(state, action) {
return {
...state,
filterList: action.payload,
isFetching: true,
dsad: "dada",
}
}
export function fetchFilterListError(state, action) {
return { ...state, isFetching: false };
}
This is where I combine them into one function(in the same file as above):
export function combinedFiltersReducers(state = initialState, action) {
switch (action.type) {
case ACTION_TYPES.FETCH_FILTER_LIST:
return fetchFilterList(state, action);
case ACTION_TYPES.FETCH_FILTER_LIST_SUCCESS:
return fetchFilterListSuccess(state, action);
case ACTION_TYPES.TOGGLE_FILTER_ITEM:
return toggleFilterItemStart(state, action);
case ACTION_TYPES.TOGGLE_FILTER_ITEM_SUCCESS:
return toggleFilterItemSuccess(state, action);
default:
return state;
}
}
This is my combine reducers function in a separate file called main reducers.
export default combineReducers({
adList: fetchAdListSuccess,
filterList: combinedFiltersReducers,
});
Here is the where I receive the state in the component, and it always falls to the initial state.
const mapStateToProps = state => ({
filterList: state.filterList,
});
const mapPropsToDispatch = dispatch => ({
fetchFilterList:() => dispatch(fetchFilterList()),
toggleFilterItem: (data) => dispatch(toggleFilterItem(data)),
});
export default connect(
mapStateToProps,
mapPropsToDispatch
)(FilterComponent);
I can not find the error, so I need a bit of help. Thanks in advance.
Okay, I have found an issue. Taking an example:
return fetchFilterList(state, action);
You're calling fetchFilterList method and passing initial state. So every time you call it actually passes the initial state. And that method is just copying the initial state. Rather do like this:
return fetchFilterList(...state, action);
I am going to assume that your ACTION_TYPES do not have the appropriate type(s) for the function you are calling so it is reverting to the default case.

Passing redux state to reducer

I'm trying to delete an element from dom by clicking on it. I did it without the problem without redux thunk but now I have a problem. My reducer doesn't know about the state. How do let him know what items are?
Action:
export function deleteItem(index) {
return {
type: 'DELETE_ITEM',
index
};
}
My reducer that shows undefined.
export function deleteItem(state = [], action) {
switch (action.type) {
case 'DELETE_ITEM':
const copy = state.items.slice()
console.log(copy)
default:
return state;
}
}
Heres my actual code https://github.com/KamilStaszewski/flashcards/tree/develop/src
I saw your code and you are defining a new reducer for each of the operations you want to get done to your items (e.i itemsHaveError, deleteItem, ...) but the correct way of doing this is to store all of the relevant functions for the items to a single reducer which holds the data needed to change whenever some action to the items happens, but in the way you did it, any time any action happens because your reducers are separated the initial state gets empty as you have passed to the functions and the reducers do not know about their related data so they overwrite them with the empty initial state, the correct way would be like this to write a single reducer for items:
const initialState = {
isLoading: false,
hasError: false,
items: [],
};
export default function(state = initialState, action) {
switch (action.type) {
case ITEMS_HAVE_ERROR:
return {
...state,
hasError: action.hasError,
};
case ITEMS_ARE_LOADING:
return {
...state,
isLoading: action.isLoading,
};
case ITEMS_FETCH_DATA_SUCCESS:
return {
...state,
items: action.items,
};
case DELETE_ITEM:
const copy = state.items.slice()
return {
...state,
items: copy,
};
default:
return state;
}
}
so this would be your item.js and your item reducer and the only one that should get to combineReducer function.
Indicate the initial State of the reducer by default , the state is an empty array and you can't access the state.items , cause it is undefined. Assume this:
const x = [];
x.foo.slice();
that would return an error . Thus from :
state = []
change it to :
state = {
items:[]
}
applying it to your code:
export function deleteItem(
state = {
items:[]
},
action) {
switch (action.type) {
case 'DELETE_ITEM':
const copy = state.items.slice()
console.log(copy)
default:
return state;
}
}

Resources