Home > Mobile >  What are the best practices for implementing the stencil store?
What are the best practices for implementing the stencil store?

Time:11-29

When creating a Stencil store, is there a best practice when defining fields?

The documentation online only has basic examples of numbers, but what if we have objects, strings or booleans? Should we be using ‘as’ or ‘new’ keywords?

Example:

const { state, onChange } = createStore({
  username: null as string,
  friends: new List<Friends>(),
  isActive: false as boolean,  
});

CodePudding user response:

The createStore() function is generic so you can create an interface to define the types:

interface StoreState {
  username: string | null;
  friends: List<Friends>;
  isActive: boolean;
}

const { state, onChange } = createStore<StoreState>({
  username: null,
  friends: new List(),
  isActive: false,
});

Note that I removed the generic type in List<Friends> because that will automatically get inferred from the interface.

  • Related