Skip to content

Dynamic "with" feature based on prop value #4684

Discussion options

You must be logged in to vote

I've applied a fix. New version:

function withCondition<
  Input extends SignalStoreFeatureResult,
  Output extends SignalStoreFeatureResult
>(
  condition: (
    store: StateSignals<Input['state']> & Input['props'] & Input['methods']
  ) => boolean,
  featureIfTrue: SignalStoreFeature<NoInfer<Input>, Output>,
  featureIfFalse: SignalStoreFeature<NoInfer<Input>, NoInfer<Output>>
): SignalStoreFeature<Input, Output> {
  return (store) => {
    const conditionStore = {
      ...store['stateSignals'],
      ...store['props'],
      ...store['methods'],
    };
    if (condition(conditionStore)) {
      return featureIfTrue(store);
    } else {
      return featureIfFalse(store);
    }
  };
}

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@blackholegalaxy
Comment options

@rainerhahnekamp
Comment options

Answer selected by blackholegalaxy
@blackholegalaxy
Comment options

@rainerhahnekamp
Comment options

@blackholegalaxy
Comment options

@markostanimirovic
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants