createEffect(name?, { handler })
Creates an effect
Arguments
name
? (string): Effect nameparams
? (Params): Setup effecthandler
(Function): function to handle effect calls, also can be set withuse(handler)
Returns
(Effect
): A container for async function.
Examples
Create unnamed effect
import {createEffect} from 'effector'
const fetchUserRepos = createEffect({
handler: async ({name}) => {
const url = `https://api.github.com/users/${name}/repos`
const req = await fetch(url)
return req.json()
}
})
Create named effect
import {createEffect} from 'effector'
const fetchUserRepos = createEffect('fetch user repositories', {
handler: async ({name}) => {
const url = `https://api.github.com/users/${name}/repos`
const req = await fetch(url)
return req.json()
}
})
Set handler to effect after creating
import {createEffect} from 'effector'
const fetchUserRepos = createEffect()
fetchUserRepos.use(async ({name}) => {
const url = `https://api.github.com/users/${name}/repos`
const req = await fetch(url)
return req.json()
})
Watch effect status
import {createEffect} from 'effector'
const fetchUserRepos = createEffect({
handler: async ({name}) => {
const url = `https://api.github.com/users/${name}/repos`
const req = await fetch(url)
return req.json()
}
})
fetchUserRepos.pending.watch(pending => {
console.log(`effect is pending?: ${pending ? 'yes' : 'no'}`)
})
fetchUserRepos.done.watch(({params, result}) => {
console.log(params) // {name: 'zerobias'}
console.log(result) // resolved value
})
fetchUserRepos.fail.watch(({params, error}) => {
console.error(params) // {name: 'zerobias'}
console.error(error) // rejected value
})
fetchUserRepos.finally.watch(({ params, status, result, error }) => {
console.log(params) // {name: 'zerobias'}
console.log(`handler status: ${status}`)
if (error) {
console.log('handler rejected', error)
} else {
console.log('handler resolved', result)
}
})
fetchUserRepos({name: 'zerobias'})
Change state
import {createStore, createEffect} from 'effector'
const fetchUserRepos = createEffect({
handler: async ({name}) => {
const url = `https://api.github.com/users/${name}/repos`
const req = await fetch(url)
return req.json()
}
})
const repos = createStore([])
.on(fetchUserRepos.done, (_, {result: repos}) => repos)