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

initialContext is not set #193

Open
BenjaminBrossi opened this issue Sep 9, 2021 · 1 comment
Open

initialContext is not set #193

BenjaminBrossi opened this issue Sep 9, 2021 · 1 comment

Comments

@BenjaminBrossi
Copy link

BenjaminBrossi commented Sep 9, 2021

Seems like i cant provide an inital context with the useMachine-hook:

const states = {
  loading: invoke<Context, QuizPublic | undefined>(
    async (...context) => {
      console.log('Context', context);
      return undefined; //getQuiz(context.language, context.quizId);
    },
    transition(
      'done',
      'loaded',
      reduce((context: Context, { data }: Event<QuizPublic>) => {
        console.log(data);
        return { ...context, quiz: data };
      })
    )
  ),
  loaded: state(
    immediate(
      'intro',
      guard((...args) => {
        console.log('Guard', args);
        return true;
      })
    ),
    immediate('')
  ),
  intro: state(),
};

export const quizMachine = createMachine<typeof states, Context>('loading', states);
const [current, send] = useMachine(quizMachine, { ...defaultContext(), quizId, language });
@mcernusca
Copy link

I know this issue is stale but I ran into this just now and maybe it helps the next person. It seems as a workaround you can explicitly pass a pass-through context fn as the last argument to createMachine because it otherwise defaults to () => {}.

const quizMachine = createMachine('loading', states, (initialContext) => initialContext);
const [current, send] = useMachine(quizMachine, { ...defaultContext(), quizId, language });

Likely a bug and not by design but I'm still wrapping my head around the code.

@matthewp matthewp transferred this issue from matthewp/react-robot Feb 2, 2023
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

2 participants