Skip to content

Component state #17

Answered by NetanelBasal
strigefleur asked this question in Q&A
Oct 11, 2021 · 1 comments · 11 replies
Discussion options

You must be logged in to vote

There are many ways to do it. Here is one of them:

const { state, config } = createState(
  withProps<{ counter: number }>({ counter: 0 })
);

@Injectable()
export class CounterRepository {
  counter$ = this.store.pipe(select(({ counter }) => counter));

  constructor(private store: Store<StoreDef<typeof state>>) { }
}

export const counterProvider = {
  provide: CounterRepository,
  useFactory() {
    return new CounterRepository(new Store({
      name: `counter-${Math.random().toFixed(3)}`,
      state,
      config,
    }))
  }
}

Replies: 1 comment 11 replies

Comment options

You must be logged in to vote
11 replies
@EricPoul
Comment options

@NetanelBasal
Comment options

@EricPoul
Comment options

@NetanelBasal
Comment options

@EricPoul
Comment options

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