Skip to content

Commit

Permalink
Update index.md (#90)
Browse files Browse the repository at this point in the history
Co-authored-by: Prokopchuk-Valentin <[email protected]>
Co-authored-by: Igor Kamyshev <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent f5c1944 commit 66c216c
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions apps/website/docs/factories/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,61 @@ const createCounter = createFactory(({ initialValue }) => {

Anywhere in your application you can invoke a factory by calling `invoke` with a factory and its arguments:

::: warning
You have to invoke factories only in the top-level of your application. It means that you **must not** invoke it during component rendering or in any other place that can be called multiple times. Otherwise, you will get a memory leak.

This limitation is applied to any factory, not only to factories created with `@withease/factories`.
:::

```ts
import { invoke } from '@withease/factories';

const counter = invoke(createCounter, { initialValue: 2 });
const { $counter, increment, decrement } = invoke(createCounter, {
initialValue: 2,
});
```

::: warning
You have to invoke factories only in the top-level of your application. It means that you **must not** invoke it during component rendering or in any other place that can be called multiple times. Otherwise, you will get a memory leak.
Now we can use `$counter`, `increment`, and `decrement` in our components. Here is how you might use them in different UI frameworks:

::: details Example usage in React

```jsx
import { useUnit } from 'effector-react';
import { $counter, increment, decrement } from './model'; // assuming you've invoked your factory in `model.js`/`model.ts`

const CounterComponent = () => {
const counter = useUnit($counter);
const [onIncrement, onDecrement] = useUnit(increment, decrement);

return (
<div>
<p>Counter: {counter}</p>
<button onClick={() => onIncrement()}>Increment</button>
<button onClick={() => onDecrement()}>Decrement</button>
</div>
);
};
```

:::

::: details Example usage in Vue

```html
<template>
<div>
<p>Counter: {{ counter }}</p>
<button @click="increment">Increment</button>
<button @click="decrement">Decrement</button>
</div>
</template>

<script setup>
import { useUnit } from 'effector-vue/composition';
import { $counter, increment, decrement } from './model'; // assuming you've invoked your factory in `model.js`/`model.ts`
const counter = useUnit($counter);
</script>
```

This limitation is applied to any factory, not only to factories created with `@withease/factories`.
:::

0 comments on commit 66c216c

Please sign in to comment.