useReducer + Typescript on a async maner - reactjs

I'm having an issue with useReducer + Typescript + async. I just can't do it! When I call anything from async function it return a Promise which break my code. When I tried to get it other way, the component is doesn't re-render! That is Driving me crazy.
I wrote this issue on my personal project which represents the problem I have! https://github.com/igormcsouza/full-stack-todo/issues/15
What I can do to make it work?
I want to make a call from the backend populate the list with the information I got from backend. So my frontend need to re-render every time any change is done to the backend (when add, update or delete any registry there).
reducers.tsx
import { delete_todo, fetch_todos, insert_todo, update_todo } from
"../utils";
import { State, Actions, Todo } from "../TodoContext";
export const INITIAL_STATE: State = {
todos: [],
};
export const reducer = (state: State, action: Actions): State => {
let newState: State = {};
switch (action.type) {
case "POPULATE":
fetch_todos().then((value) => (newState = value));
return newState;
case "ADD_TODO":
if (state.todos) {
const newTodo: Todo = {
when: (+new Date()).toString(),
task: action.payload,
checked: false,
by: "Igor Souza",
};
insert_todo(newTodo);
}
fetch_todos().then((value) => (newState = value));
return newState;
case "CHECK_TODO":
action.payload.checked = !action.payload.checked;
update_todo(action.payload);
fetch_todos().then((value) => (newState = value));
return newState;
case "EDIT_TODO":
let todo = action.payload.task;
todo.task = action.payload.newTaskName;
update_todo(todo);
fetch_todos().then((value) => (newState = value));
return newState;
case "DELETE_TODO":
delete_todo(action.payload);
fetch_todos().then((value) => (newState = value));
return newState;
default:
return state;
}
};
utils.tsx (with the axios calls)
import axios from "axios";
import { State, Todo } from "./TodoContext";
// const base = "http://backend:2500";
const base = "https://full-stack-todo-bknd.herokuapp.com";
export async function fetch_todos(): Promise<State> {
let todos: State = {};
await axios
.get<State>(base + "/api/todo")
.then((response) => {
const { data } = response;
todos = data;
})
.catch((e) => console.log(e));
console.log(typeof todos.todos);
return todos;
}
export async function insert_todo(todo: Todo) {
await axios.post(base + "/api/todo", todo).catch((e) => console.log(e));
}
export async function update_todo(todo: Todo) {
await axios.put(base + "/api/todo/" + todo.id).catch((e) => console.log(e));
}
export async function delete_todo(todo: Todo) {
await axios
.delete(base + "/api/todo/" + todo.id)
.catch((e) => console.log(e));
}
context.tsx (Context APi)
import React, { createContext, useReducer } from "react";
import { reducer, INITIAL_STATE } from "./reducers";
type ContextProps = {
state: State;
dispatch: (actions: Actions) => void;
};
export interface Todo {
id?: string;
task: string;
when: string;
checked: boolean;
by: string;
}
export interface State {
todos?: Array<Todo>;
}
export interface Actions {
type: string;
payload?: any;
}
export const TodoContext = createContext<Partial<ContextProps>>({});
const TodoContextProvider: React.FC = ({ children }) => {
const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
return (
<TodoContext.Provider value={{ state, dispatch }}>
{children}
</TodoContext.Provider>
);
};
export default TodoContextProvider;

Put simply, what you are trying to do is not possible. You cannot have a reducer that is asynchronous. This means that you need to move the async logic outside of the reducer itself.
The reducer is just responsible for applying the data from the action to the state. Since you are re-fetching the whole list after every action (not ideal) you only have one real action which is to replace the whole state. You would do the aysnc fetching and then refresh the state.
export const populate = (dispatch: Dispatch<Actions>) => {
fetch_todos().then((data) =>
dispatch({
type: "POPULATE",
payload: data
})
);
};
export const reducer = (state: State, action: Actions): State => {
switch (action.type) {
case "POPULATE":
return action.payload;
...
<button onClick={() => populate(dispatch)}>Populate</button>
Passing the dispatch function to an action creator is called a "thunk" and it's a popular pattern with Redux. We don't have any middleware, so we just directly call populate(dispatch) instead of something like dispatch(populate()).
Look for ways that you can streamline your code.
We can make use of the fact that all our actions call the same fetch_todos() in order to simplify things (for now -- eventually you want to not refresh the entire list after every change).
insert_todo, update_todo, and delete_todo are all extremely similar. The main difference is the axios method which can be passed as an argument with axios.request.
Though the more I look, the more I see that they should be less similar! You need to pass the todo data on your put request. You want the id property on Todo to be required and for add_todo to take Omit<Todo, 'id'>.
The inverted approach would be to make changes directly to the reducer state first. Then use a useEffect to detect changes and push the to the backend.

Related

infinite loop with react/redux

I have tiredlessly tried everything i can find on stack for this issue and am getting no where. We are using react/typescript. redux, and saga. I have a list of categories to bring back for nav list and using useEffect to dispatch the action to redux store.
our tsx.file:
const dispatch = useDispatch();
const categories = useSelector((state) => state?.categories?.payload);
const loadCategories = () => {
dispatch(getCategories(categories));
};
useEffect(() => {
loadCategories();
}, []);
{categories?.map((x, index) => (
<Link href={"/store/" + `${x.name}` + "/s"}>
<a
type="button"
id={`${x.name}`}
title={`${x.name}`}
className={"xl:px-3 px-2 py-[1.15rem] font-normal"}>
{x.name}
</a>
</Link>
))}
Network traffic just shows hundreds of requests going out to the category endpoint -- stumped!
still stuck so adding our redux/saga files
actions:
import {GET_CATEGORIES} from './actionTypes'
export const getCategories = (categories: any) => {
return {
type: GET_CATEGORIES,
payload: categories,
}
}
reducer:
import {GET_CATEGORIES} from './actionTypes'
const reducer = (state = [], action) => {
switch (action.type) {
case GET_CATEGORIES:
state = {
...state,
payload: action.payload,
}
break
default:
state = {...state}
break
}
return state
}
export default reducer
saga:
let categoriesApiService = container.resolve(CategoriesApiService)
const categoryApi = async () => {
return firstValueFrom(
categoriesApiService.GetCategoryTree({
path: {version: '1'},
query: {},
})
)
}
function* getCategoriesTree() {
try {
let categoryTreeDTO: CategoryTreeDTO = yield call(categoryApi)
yield put(getCategories(categoryTreeDTO))
} catch (error: any) {
yield put(apiError(error?.response?.data?.message))
}
}
export function* watchGetCategories() {
yield takeEvery(GET_CATEGORIES, getCategoriesTree)
}
function* categorySaga() {
yield all([fork(watchGetCategories)])
}
export default categorySaga
The problem is that you are using the same action to start the saga & to store the fetched data:
component: dispatch(getCategories(categories))
saga: yield put(getCategories(categoryTreeDTO))
so every time data are fetched the saga is triggered again.
What you want to do is to have different action for storing the fetched data, e.g.:
yield put(getCategoriesSuccess(categoryTreeDTO))
(also update your action type in reducer)

Exporting extra reducers from redux toolkit

I made a todo list a while ago as a way to practice react and redux. Now I'm trying to rewrite it with redux toolkit and having some trouble with the action creators.
Here is the old actions creator:
export const changeDescription = (event) => ({
type: 'DESCRIPTION_CHANGED',
payload: event.target.value })
export const search = () => {
return (dispatch, getState) => {
const description = getState().todo.description
const search = description ? `&description__regex=/${description}/` : ''
axios.get(`${URL}?sort=-createdAt${search}`)
.then(resp => dispatch({ type: 'TODO_SEARCHED', payload: resp.data }))
} }
export const add = (description) => {
return dispatch => {
axios.post(URL, { description })
.then(() => dispatch(clear()))
.then(() => dispatch(search()))
} }
export const markAsDone = (todo) => {
return dispatch => {
axios.put(`${URL}/${todo._id}`, { ...todo, done: true })
.then(() => dispatch(search()))
} }
export const markAsPending = (todo) => {
return dispatch => {
axios.put(`${URL}/${todo._id}`, { ...todo, done: false })
.then(() => dispatch(search()))
} }
export const remove = (todo) => {
return dispatch => {
axios.delete(`${URL}/${todo._id}`)
.then(() => dispatch(search()))
} }
export const clear = () => {
return [{ type: 'TODO_CLEAR' }, search()] }
Now this is the one that I'm working on, I'm trying to replicate the actions of the old one but using redux toolkit:
export const fetchTodos = createAsyncThunk('fetchTodos', async (thunkAPI) => {
const description = thunkAPI.getState().todo.description
const search = description ? `&description__regex=/${description}/` : ''
const response = await axios.get(`${URL}?sort=-createdAt${search}`)
return response.data
})
export const addTodos = createAsyncThunk('fetchTodos', async (thunkAPI) => {
const description = thunkAPI.getState().todo.description
const response = await axios.post(URL, {description})
return response.data
})
export const todoReducer = createSlice({
name: 'counter',
initialState: {
description: '',
list: []
},
reducers: {
descriptionChanged(state, action) {
return {...state, dedescription: action.payload}
},
descriptionCleared(state, action) {
return {...state, dedescription: ''}
},
},
extraReducers: builder => {
builder
.addCase(fetchTodos.fulfilled, (state, action) => {
const todo = action.payload
return {...state, list: action.payload}
})
.addCase(addTodos.fulfilled, (state, action) => {
let newList = state.list
newList.push(action.payload)
return {...state, list: newList}
})
}
})
The thing is, I can't find anywhere how to export my extra reducers so I can use them. Haven't found anything in the docs. Can someone help?
extraReducers
Calling createSlice creates a slice object with properties reducers and actions based on your arguments. The difference between reducers and extraReducers is that only the reducers property generates matching action creators. But both will add the necessary functionality to the reducer.
You have correctly included your thunk reducers in the extraReducers property because you don't need to generate action creators for these, since you'll use your thunk action creator.
You can just export todoReducer.reducer (personaly I would call it todoSlice). The reducer function that is created includes both the reducers and the extra reducers.
Edit: Actions vs. Reducers
It seems that you are confused by some of the terminology here. The slice object created by createSlice (your todoReducer variable) is an object which contains both a reducer and actions.
The reducer is a single function which takes the previous state and an action and returns the next state. The only place in your app when you use the reducer is to create the store (by calling createStore or configureStore).
An action in redux are the things that you dispatch. You will use these in your components. In your code there are four action creator functions: two which you created with createAsyncThunk and two which were created by createSlice. Those two will be in the actions object todoReducer.actions.
Exporting Individually
You can export each of your action creators individually and import them like:
import {fetchTodos, descriptionChanged} from "./path/file";
Your fetchTodos and addTodos are already exported. The other two you can destructure and export like this:
export const {descriptionChanged, descriptionCleared} = todoReducer.actions;
You would call them in your components like:
dispatch(fetchTodos())
Exporting Together
You might instead choose to export a single object with all of your actions. In order to do that you would combine your thunks with the slice action creators.
export const todoActions = {
...todoReducer.actions,
fetchTodos,
addTodos
}
You would import like this:
import {todoActions} from "./path/file";
And call like this:
dispatch(todoActions.fetchTodos())

how to render updated state in react-redux?

I am trying to update state and render the updated value in my component but unable to achieve it.
<--Here is my action -->
export const fetchProducts = () => async (dispatch) => {
const res = await fakeApi.get("/products");
console.log(res.data);
dispatch({
type: FETCH_PRODUCTS,
payload: res.data,
});
};
<--Here is the reducer -->
const initialState = {
products: [],
};
export default (state = initialState, action) => {
const { type, payload } = action;
switch (type) {
case FETCH_PRODUCTS:
return { ...state, products: payload };
default:
return state;
}
};
<--Here is the component -->
const Products = ({ products, fetchProducts }) => {
const [productsList,setProductsList]=useState([])
useEffect(() => {
fetchProducts();
console.log(products);
setProductsList({productsList:products})
}, []);
return <ProductList products={productsList} />;
};
const mapStateToProps = (state) => ({
products: state.products,
});
export default connect(mapStateToProps, { fetchProducts })(Products);
<-- This is the first time I am using redux. I don't know where I am going wrong.All i am getting is undefined in the state. -->
you are using react hooks for a function-based component, using useSelector() hook instead of mapStateToProps might be easier in this specific scenario to get data from redux store, as i can't see what you've named your reducer in the root reducer, i'm assuming it's named productReducer, try this in your component:
import {useSelector} from 'react-redux'
...
let products = useSelector(state=>state.productReducer.Products)

Empty Array inside Redux Action map object

I'm getting an object from an action (using axios) and using a map function to iterate it.
I also need to get another action but inside the parent object mapped.
I see that the request/response are ok (with returned data), but the reducer variable still gets empty.
1: component gets data
componentDidMount() {
const { match: { params } } = this.props;
this.props.getSaleDetails(params.order_id);
}
2: defining mapStateToProps and mapDispatchToProps
const mapStateToPropos = state => ({
saleDetails: state.salesOrders.saleDetails,
saleDetailFurthers: state.salesOrders.saleDetailFurthers
});
const mapDispatchToProps = dispatch =>
bindActionCreators({ getSaleDetails, getDetailFurthers }, dispatch);
3: creating a const from the redux props
const detailsArray = saleDetails.data;
4: iterate array with map function
// getDetailFurthers is another action, getting data by passing "detail_id" and updating "saleDetailFurthers" props
{detailsArray && detailsArray.map((item) => {
const {getDetailFurthers, saleDetailFurthers} = this.props;
getDetailFurthers(item.detail_id)
console.log(saleDetailFurthers)
// empty array????
count++;
return (
<Paper className={classes.paper} key={item.detail_id}>
// ... next code lines
5: Actions
export function getDetailFurthers(detail_id){
return async dispatch => {
const request = await axios.get(`${BASE_URL}/salesorders/detail/furthers/${detail_id}`)
return {
type: "SALE_DETAIL_FURTHERS_FETCHED",
payload: request
}
}
}
6: Reducers
const INITIAL_STATE = {
//... others
saleDetailFurthers: []
};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
///... other cases
case "SALE_DETAIL_FURTHERS_FETCHED":
return { ...state, saleDetailFurthers: action.payload }
default:
return state
}
};
I expect the "saleDetailFurthers" const be loaded with data from redux action.
You need to use dispatch instead of returning, like so:
dispatch({
type: "SALE_DETAIL_FURTHERS_FETCHED",
payload: request
});
export function getDetailFurthers(detail_id) => dispatch =>{
const request = await axios.get(`${BASE_URL}/salesorders/detail/furthers/${detail_id}`)
dispatch ({
type: "SALE_DETAIL_FURTHERS_FETCHED",
payload: request
})
}

How can I keep previous state and add a new state to the previous state in Redux reducers?

The below is one of my reducers. In the function applySetRandomImages, I want to access previous state of randomImages to add a new state of randomImages with the previous state.
How can I do that? does the reducer function in Redux provides some call back function for that? or should I implement that on my own?
// ACTIONS
const SET_RANDOM_IMAGES = "SET_RANDOM_IMAGES";
// ACTION CREATORS
function setRandomImages(randomImages) {
return {
type: SET_RANDOM_IMAGES,
randomImages
};
}
// API ACTIONS
function getRandomImages(page) {
return (dispatch, getState) => {
fetch(`/boutiques/random-images/?page=${page}`)
.then(response => response.json())
.then(json => dispatch(setRandomImages(json.results)))
.catch(err => console.log(err));
};
}
// INITIAL STATE
const initialState = {};
// REDUCER
function reducer(state = initialState, action) {
switch (action.type) {
case SET_RANDOM_IMAGES:
return applySetRandomImages(state, action);
default:
return state;
}
}
// REDUCER FUNCTIONS
function applySetRandomImages(state, action) {
const { randomImages } = action;
return {
...state,
randomImages <--- I need to merge the randomImages with a new state of randomImages
};
}
// EXPORTS
const actionCreators = {
getRandomImages,
};
export { actionCreators };
// DEFAULT REDUCER EXPORTS
export default reducer;
You can merge randomImages by spreading the old state and the new one into a new array:
function applySetRandomImages(state, action) {
const { randomImages } = action;
return {
...state,
randomImages: [...state.randomImages, ...randomImages],
};
}
Separate actions, reducers and types into their own folders.
types/index.js
export const SET_RANDOM_IMAGES = "SET_RANDOM_IMAGES";
actions/imageActions.js
import * as types from '../types';
export const getRandomImages = page => dispatch => (
fetch(`/boutiques/random-images/?page=${page}`)
.then(response => response.json())
.then(json => dispatch({ type: types.SET_RANDOM_IMAGES, payload: json.results })))
.catch(err => console.log(err))
)
From within a component, you will connect to redux state (state.images or state.images.collection) and dispatch the action (getRandomImages):
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getRandomImages } from '../actions/imageActions';
class Example extends Component {
componentDidMount = () => this.props.getRandomImages(); // dispatches action creator
render = () => (
<div>
{ /*
The props below are redux state passed into
the component via the connect function
*/ }
{this.props.images.map(image => (
<img src={image.src} alt="image.name" />
))}
{this.props.collection.map(image => (
<img src={image.src} alt="image.name" />
))}
</div>
)
}
export default connect(state => ({
images: state.images, // pulled from redux state (state.images => this.props.images)
collection: state.images.collection // pulled from redux state (state.images.collection => this.props.images.collection)
}), { getRandomImages})(Example)
It will then trigger the AJAX request, then return a type and payload to your reducer:
reducers/index.js
import * as types from '../types'
// overwrite images for each successful fetch request
const imageReducer(state={}, {type, payload}) {
switch (type) {
// spread out any previous state, then spread out the payload (json.results)
case types.SET_RANDOM_IMAGES: return { ...state, ...payload }
default: return state;
}
}
// or append images on each successful fetch request...
const imageReducer(state={}, {type, payload}) {
switch (type) {
case types.SET_RANDOM_IMAGES:
return {
...state, // spread out any previous state
collection: [
...state.collection, // then spread out any previous "collection" state,
...payload // then spread/append the payload (json.results)
]
}
default: return state;
}
}
export default combineReducers({
images: imageReducer
});
The reducer will then spread out any previous imageReducer state, then append the res.results to it via payload. Now it exists in redux as state.images or state.images.collection. This is then pulled from redux state and into the component above as this.props.images or this.props.images.collection.
I want to access previous state of randomImages to add a new state of randomImages with the previous state
return {
...state,
...randomImages
};
If previous state was:
{
a: 1,
b: 2,
c: 3
}
And randomImages is:
{
d: 4,
e: 5
}
Then the returned new state will be
{
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
}

Resources