Skip to content

Function: hasContext()

API / @xmachines/play-xstate / hasContext

function hasContext<TContext>(path): Guard<TContext, PlayEvent>;

Defined in: packages/play-xstate/src/guards/helpers.ts:24

Tells you if the context holds a value at the path, and that the value is truthy

Type Parameters

Type ParameterDefault type
TContextRecord<string, unknown>

Parameters

ParameterTypeDescription
pathstringThe path to the context property, with a dot between two segments

Returns

Guard<TContext, PlayEvent>

The guard predicate. It tests the property for a truthy value

Example

const machine = setup({
guards: {
hasUserId: hasContext("userId"),
hasEmail: hasContext("user.email"),
},
});

Deprecated

This function is part of the guard utilities, and the next major version removes them. Write a plain typed predicate instead.