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

Bug: sendParent throws an error when no parent exists #5120

Open
dhensby opened this issue Nov 11, 2024 · 2 comments
Open

Bug: sendParent throws an error when no parent exists #5120

dhensby opened this issue Nov 11, 2024 · 2 comments
Labels

Comments

@dhensby
Copy link

dhensby commented Nov 11, 2024

XState version

XState version 5

Description

The docs for sendParent() state (emphasis added):

The sendParent(...) action is a special action that sends an event to the parent actor, if it exists.

However, when running sendParent() as an action an error is thrown (Error: Unable to send event to actor '#_parent' from machine '(machine)'.).

Code to reproduce:

import { createActor, sendParent, setup } from 'xstate';

const machine = setup({}).createMachine({
    initial: 'init',
    states: {
        init: {
            entry: sendParent(() => ({ type: 'init' })),
        },
    },
});

const actor = createActor(machine);
actor.start();

Expected result

I expect that no error is thrown and the sendParent() action becomes a no op when no parent exists.

Actual result

The following error is thrown: Error: Unable to send event to actor '#_parent' from machine '(machine)'.

Reproduction

see description

Additional context

Reproduced with [email protected]

@dhensby dhensby added the bug label Nov 11, 2024
@davidkpiano
Copy link
Member

This is currently by design - it is a breaking change to change this to a warning instead of an error currently 🤔

@dhensby
Copy link
Author

dhensby commented Nov 11, 2024

Fair enough - I suppose this is more a docs issue, then?

Is there a way to nicely achieve this (optional send to parent)? Or maybe the options sent to the sendParent could have a new property to allow it to be optional? eg: sendParent(() => ({ type: 'init' }), { allowMissingParent: true })

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

No branches or pull requests

2 participants