logo dile-components dile-components

State Mixin

This mixin enables any application component to subscribe to Redux store state changes. By implementing the mixin, components automatically receive state updates and can respond to any changes in the store.

Usage

To use this mixin, you must have @dile/lib installed as described on the @dile/lib homepage.

It works through Redux, so you need to implement the Redux store provided by @dile/lib, as explained in the Redux implementation guide.

Once you have the store in your application, import it into the component where you want to use DileState and apply the mixin by providing your application's store.

Here's an example:

import { store } from '../redux/store.js';
import { DileState } from '@dile/lib';

export class DileApp extends DileState(store)(LitElement) {
   stateChanged(state) {
      // Handle state changes here
   }
}

How it works

The DileState mixin automatically subscribes to the Redux store when the component is connected to the DOM and unsubscribes when disconnected. Every time the store state changes, the stateChanged() method is called with the current state.

Available methods

The DileState mixin adds methods to the component for managing state subscriptions. Below are the methods, their signatures, descriptions, and usage examples.

stateChanged(state)

Example:

stateChanged(state) {
  this.userData = state.user.userData;
  this.isLoading = state.feedback.loading;
}

getState()

Example:

const currentState = this.getState();
console.log(currentState.user.isLoggedIn);

Lifecycle

The DileState mixin manages the Redux subscription through the component's lifecycle:

This ensures efficient state management and proper cleanup of subscriptions.

Best practices