Replies: 2 comments 4 replies
-
Hello @mkhoussid ! You don't really need Events are already behave like regular functions to the rest of the world around them. As of someFunction, I would recommend that you start breaking this function down into its individual meaningful parts, something like that: const getSomeDataFx = createEffect(async () => httpClient({
url: '/api/some-endpoint',
method: EMethodTypes.GET,
}))
const startSomething = createEvent()
const $someData = createStore(null)
sample({
clock: startSomething,
target: getSomeDataFx
})
sample({
clock: getSomeDataFx.doneData,
target: $someData
}) Notice, how it is all separated declarative blocks and unit definitions - it is easier to read and to change, as you no longer bound to single function. sample({
source: $someOtherData,
clock: getSomeDataFx.doneData,
filter: (_, someData) => shouldSkip(someData),
fn: (someOtherData, someData) => mixData(someOtherData, someData),
target: $someData
}) Also notice, that const getSomeDataFx = createEffect(async () => httpClient({
url: '/api/some-endpoint',
method: EMethodTypes.GET,
}))
// use both in effector's logic
sample({
clock: buttonClicked,
filter: $moonPhaseIsRight,
target: getSomeDataFx,
})
// and old redux stuff for soft migration
export const someFunction = (): ThunkAction =>
async (dispatch): Promise<void> => {
try {
const { someData } = await getSomeDataFx()
setSomeDataEvent(someData);
} catch (err) {
console.log('Error in someFunction', err);
}
}; |
Beta Was this translation helpful? Give feedback.
-
There are bit more examples in the documentation: Also you might find useful some of effector's ecosystem libraries like
Patronum also includes |
Beta Was this translation helpful? Give feedback.
-
I've decided to try
effector
and I'm trying to figure out the best way to replace redux in my project with it.Whenever I use redux in a React project, I usually have the following structure for a feature:
With the said, here is how my files would look like:
My actions:
My reducer:
And
types.ts
would haveexport const SOME_ACTION = '@redux/features/some_feature/some-action'
.Anyway, here is how my folder structure looks like now:
And here are the files:
So, it's already cleaner and less code. The reason I've chosen such a structure and approach, is because it is very similar to my existing structure.
Anyway, effector offers different ways of mutating stores, one of which is on
doneData
:Here, once
getCommentsFx
finishes executing, the value for the store$postComments
is set with whatevergetCommentsFx.doneData
resolves to.What I'm having trouble with, is utilizing the same approach, but making it "friendly" with my current project.
The only way that I can think of, is rewriting
someFunction
like this:But I see no point in using
createEffect
at all, because I can just do this:Any suggestions about going about this? Is it fine to use
effector
withoutcreateEffect
(or most other provided methods via its API, for that matter)? Essentially, I'm just creating stores and binding events to them, I feel like I'm not usingeffector
in the way it was intended, but I can't think of a better way to rewrite it.What can I do here? Should I just got back to Redux?
Beta Was this translation helpful? Give feedback.
All reactions