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

Explain how .provide() can not be called imperatively to override implementations #142

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
29 changes: 27 additions & 2 deletions versioned_docs/version-5/machines.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const feedbackMachine = createMachine(
delays: {
/* ... */
},
}
},
);
```

Expand Down Expand Up @@ -143,6 +143,31 @@ const feedbackActor = interpret(customFeedbackMachine).start();
// logs 'Doing something custom!'
```

:::warningxstate

`machine.provide()` is not imperative but rather returns a new machine that includes the implementation overrides.

Modifying the previous example, if we were to imperatively call `.provide()` on an existing machine, that would **not** change the machine’s behavior with our implementations.

```ts
// Do NOT do this!
const feedbackMachine = createMachine({...});

feedbackMachine.provide({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Assign this to a new machine var and pass to createActor(newMachine)

actions: {
doSomething: () => {
console.log('Doing something custom!');
},
},
});

// This will NOT utilize the provided implementations.
const feedbackActor = interpret(feedbackMachine).start();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be createActor.


```

:::

## Specifying types

You can specify TypeScript types inside the machine config using the `.types` property:
Expand Down Expand Up @@ -246,4 +271,4 @@ const machineWithImpls = machine.provide({
/* ... */
},
});
```
```