Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.md #90

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion apps/website/docs/factories/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,75 @@ const createCounter = createFactory(({ initialValue }) => {
### `invoke`

Anywhere in your application you can invoke a factory by calling `invoke` with a factory and its arguments:
::: warning
It is recommended to call invoke in the same places where Effector code is written, rather than inside components.
:::


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

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

Now we can use `$counter`, `increment`, and `decrement` in our components. Here’s how you might use them in different UI frameworks:

::: details Example usage in React
```jsx
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';
import { $counter, increment, decrement } from './store'; // assuming you've invoked your factory in `store.js`

const counter = useUnit($counter);
</script>
```
:::

::: details Example usage in Svelte
```svelte
<script>
import { useUnit } from 'effector-svelte';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Но ведь пакета effector-svelte не существует?..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

удалил, да, по svelte ошибся

import { $counter, increment, decrement } from './store'; // assuming you've invoked your factory in `store.js`

const counter = useUnit($counter);
</script>

<div>
<p>Counter: {$counter}</p>
<button on:click="{increment}">Increment</button>
<button on:click="{decrement}">Decrement</button>
</div>
```
:::

::: 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`.
:::

Loading