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

Nested states guide incorrect about assigning machines to states #129

Open
Ghirigoro opened this issue Sep 18, 2020 · 2 comments
Open

Nested states guide incorrect about assigning machines to states #129

Ghirigoro opened this issue Sep 18, 2020 · 2 comments

Comments

@Ghirigoro
Copy link

Ghirigoro commented Sep 18, 2020

In the guide there's this example:

// A helper function for more easily building nested state machines.
const nested = (to, states) => createMachine(states,
  transition('done', to)
);

const stoplight = createMachine({
  green: state(
    transition('next', 'yellow')
  ),
  yellow: state(
    transition('next', 'red')
  ),
  red: nested('green', {
    walk: state(
      transition('toggle', 'dontWalk')
    ),
    dontWalk: final()
  })
});

This will result in an error if you try to transition to the nested state, e.g.:

const service = interpret(stoplight, ()=>{})
service.send('next') // service.machine.current === 'yellow'
service.send('next') // ERROR: state.enter is not a function

In the earlier part of the guide (and elsewhere) it states that you should use 'invoke' to assign a sub-machine to a state, but in the example a machine is assigned directly.

Was the intention to write something like this for the nested function?

const nested = (to, states) => invoke(createMachine(states), transition('done', to));
@jesperp
Copy link

jesperp commented Oct 27, 2020

Also got stuck for a while in the guide there. But yeah, I think the intention definitely was something like:

const nested = (to, states) => invoke(
  createMachine(states),
  transition('done', to),
);

@Zearin
Copy link
Contributor

Zearin commented Aug 22, 2021

@matthewp Want to tag this Issue Docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants