Skip to main content

Function: withConditional()

withConditional<Input, Output>(condition, featureIfTrue, featureIfFalse): SignalStoreFeature<Input, Output>

Defined in: libs/ngrx-toolkit/src/lib/with-conditional.ts:52

withConditional activates a feature based on a given condition.

Use Cases

  • Conditionally activate features based on the store state or other criteria.
  • Choose between two different implementations of a feature.

Type Constraints Both features must have exactly the same state, props, and methods. Otherwise, a type error will occur.

Usage

const withUser = signalStoreFeature(
withState({ id: 1, name: 'Konrad' }),
withHooks(store => ({
onInit() {
// user loading logic
}
}))
);

function withFakeUser() {
return signalStoreFeature(
withState({ id: 0, name: 'anonymous' })
);
}

signalStore(
withMethods(() => ({
useRealUser: () => true
})),
withConditional((store) => store.useRealUser(), withUser, withFakeUser)
)

Type Parameters

Input extends SignalStoreFeatureResult

Output extends SignalStoreFeatureResult

Parameters

condition

(store) => boolean

A function that determines which feature to activate based on the store state.

featureIfTrue

SignalStoreFeature<NoInfer<Input>, Output>

The feature to activate if the condition evaluates to true.

featureIfFalse

SignalStoreFeature<NoInfer<Input>, NoInfer<Output>>

The feature to activate if the condition evaluates to false.

Returns

SignalStoreFeature<Input, Output>

A SignalStoreFeature that applies the selected feature based on the condition.