I'm facing the problem with the effector (state manager)
- The user is signing in and then I'm making the get request to get user info
- I need to store that info in the effector store to use it later
This is my models/user/index.ts :
import { combine, createEffect, createEvent, createStore, restore } from 'effector';
import { User } from './types';
export const $user = createStore<User[]>([]);
export const fetchUserInfoFx = createEffect<void, User, Error>();
This is models/user/init.ts
import { $user, fetchUserInfoFx } from '.';
fetchUserInfoFx.use(async () => {
const response = await fetch('http://localhost:5000/auth/me', { credentials: 'include' });
const data = await response.json();
const id = data.id === null ? '' : data.id;
const username = data.username === null ? '' : data.username;
const fullname = data.fullname === null ? '' : data.fullname;
return { id, username, fullname };
});
$user.on(fetchUserInfoFx.doneData, (data) => data);
This is models/user/types.ts :
export type User = {
id: string;
username: string;
fullname: string;
};
Could you please help ? I'm new to effector, So don't know why I can't fetch user info into the store. Moreover, I'm 100% sure that I get all the values from the fetch call. Just don't know how to pass them into the store. Thanks!
Here are the docs for
$store.on(trigger, reducer).In your example reducer, you never modify the store (you just return it every time the reducer is invoked), so the data in the store never changes, no matter how many times the effect is run. This is how you can append users to the array using an effect:
TS Playground