question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add documentation on starting the machine

See original GitHub issue

Took me a while to realize that calling useMachine does not start the machine. Is there a good reason to leave that up to the caller? The machine is stopped when unmounting, so maybe it makes sense to start it when it is first called? If not, then it would be nice to document this. FWIW, I had to add this code to start my machine:

const machine = useMachine(...);

useEffect(() => {
  machine.service.start();
}, []);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
bschlenkcommented, Jan 19, 2019

I found the issue. onTransition and onChange are called after init, so anything that happens during initialization is missed. Calling init after both callbacks are set fixes the problem.

1reaction
bschlenkcommented, Jan 19, 2019

I created a minimal example, but this may actually be an issue with xstate, it looks like it isn’t calling onChange for actions triggered during initialization.

https://codesandbox.io/s/4zjv035mo9

I took your example and wrapped it in two outer states: setup and main. I also added a name field to the context that defaults to "unset". Setup has an onEntry action that updates name, and then an immediate transition to main. If you look at the output, name doesn’t get updated as expected. But if you uncomment the machine.service.start call on line 86, the state does update.

If you leave the start call commented and click one of the tick buttons, you can see that name does update. So the state machine is holding the correct state, it just isn’t notifying react that the state context has changed during initialization.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Steps to Create Technical Documentation That's (Actually ...
So if you don't know where to start, here's our short guide to making technical documentation that's actually helpful.
Read more >
How to Write a Good Documentation: Home
Best Practices for Documenting Your Project · Include a README file that contains · Allow issue tracker for others · Write an API...
Read more >
How to Build the Best User Documentation (New Guide) | Blog
Have a product or service that needs user documentation? Here are some simple tips on how to build the best documentation for consumers....
Read more >
The importance of machine documentation
"Mastering the Machine" describes the process of concepting, procuring, designing, building and documenting a machine or system from the ...
Read more >
How to Manage all Your Equipment Documentation | GoCodes
Try these tips to establish a winning equipment documentation management system, keep your business compliant, and your equipment in top condition.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found