Answer the question
In order to leave comments, you need to log in
Do I need to write the data from the form in redux before submitting it to the server?
Hello, the question arose whether it is necessary to write data from the form in redux before sending it to the server?
I'm using redux toolkit along with redux saga, what strategy should i follow when sending data to server?
How to correctly transfer payload to saga Worker from a form, and is payload needed in toolkit?
I would be grateful for the help
TOOLKIT
const usersSlice = createSlice({
name: 'users',
initialState: {
data: undefined,
isAuth: false,
loginError: '',
},
reducers: {
fetchLogin: (_, { payload }) => payload,
setUserData: (state, action) => {
state.data = action.payload
},
setAuth: (state, action) => {
state.isAuth = action.payload
},
setLoginError: (state, action) => {
state.loginError = action.payload
},
},
})
export default usersSlice.reducer
export const { setUserData, setAuth, setLoginError, fetchLogin } = usersSlice.actions
export function* fetchLoginWorker({ payload }: any): Generator<any> {
try {
console.log(payload)
const response: any = yield call(AuthApi.login, payload)
localStorage.setItem('token', response.data.accessToken)
yield put(setAuth(true))
yield put(setUserData(response.data.user))
} catch (e) {
console.log(e.response?.data?.message)
yield put(setLoginError(e.response?.data?.message))
}
}
export function* usersSaga() {
yield takeEvery(fetchLogin.type, fetchLoginWorker)
}
const onSubmit: SubmitHandler<ILoginInputs> = async (data) => {
dispatch(fetchLogin(data))
}
Answer the question
In order to leave comments, you need to log in
Not necessarily, in fact, you can not write anything anywhere at all, but send data directly from the form component to the server.
If it makes sense to share this data with other components, it makes sense to write everything to the store.
On the other hand, some old people say that it's better to store everything in the store and not have local states (this is a very controversial approach, as for me)
The only option that can be advised without a doubt is to think about UX and add a record of non-private user data to localStorage , so that in the case of a page refresh, they would return. (You can also use autocomplete, but it's been kind of mad lately, especially in chrome)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question