@edsilv/stencil-redux

中文文档
0.1.2发布于 2020/05/21MITTypeScriptESM + CJS零依赖近 30 天下载 1.2万

Stencil Redux - A simple redux-connector for Stencil-built web components

原始使用文档
文档目录9 节
  1. Stencil Redux
  2. Install
  3. Usage
  4. Configure the Root Reducer
  5. Configure the Actions
  6. Configure the Store
  7. Configure Store in Root Component
  8. Map state and dispatch to props
  9. Usage with redux-thunk

Stencil Redux

A simple redux connector for Stencil-built web components inspired by react-redux.

Install

npm install @stencil/redux
npm install redux

Usage

Stencil Redux uses the redux library underneath. Setting up the store and defining actions, reducers, selectors, etc. should be familiar to you if you've used React with Redux.

Configure the Root Reducer

// redux/reducers.ts

import { combineReducers } from 'redux';

// Import feature reducers and state interfaces.
import { TodoState, todos } from './todos/reducers';

// This interface represents app state by nesting feature states.
export interface RootState {
  todos: TodoState;
}

// Combine feature reducers into a single root reducer
export const rootReducer = combineReducers({
  todos,
});

Configure the Actions

// redux/actions.ts

import { RootState } from './reducers';

// Import feature action interfaces
import { TodoAction } from './todos/actions';

// Export all feature actions for easier access.
export * from './todos/actions';

// Combine feature action interfaces into a base type. Use union types to
// combine feature interfaces.
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types
export type Action = (
  TodoAction
);

Configure the Store

// redux/store.ts

import { Store, applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk'; // add-on you may want
import logger from 'redux-logger'; // add-on you may want

import { RootState, rootReducer } from './reducers';

export const store: Store<RootState> = createStore(rootReducer, applyMiddleware(thunk, logger));

Configure Store in Root Component


// components/my-app/my-app.tsx

import { Store } from '@stencil/redux';

import { Action } from '../../redux/actions';
import { RootState } from '../../redux/reducers';
import { store } from '../../redux/store';

@Component({
  tag: 'my-app',
  styleUrl: 'my-app.scss'
})
export class MyApp {
  @Prop({ context: 'store' }) store: Store<RootState, Action>;

  componentWillLoad() {
    this.store.setStore(store);
  }
}

Map state and dispatch to props

:memo: Note: Because the mapped props are technically changed within the component, mutable: true is required for @Prop definitions that utilize the store. See the Stencil docs for info.

// components/my-component/my-component.tsx

import { Store, Unsubscribe } from '@stencil/redux';

import { Action, changeName } from '../../redux/actions';
import { RootState } from '../../redux/reducers';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.scss'
})
export class MyComponent {
  @Prop({ context: 'store' }) store: Store<RootState, Action>;
  @Prop({ mutable: true }) name: string;

  changeName!: typeof changeName;

  unsubscribe!: Unsubscribe;

  componentWillLoad() {
    this.unsubscribe = this.store.mapStateToProps(this, state => {
      const { user: { name } } = state;
      return { name };
    });

    this.store.mapDispatchToProps(this, { changeName });
  }

  componentDidUnload() {
    this.unsubscribe();
  }

  doNameChange(newName: string) {
    this.changeName(newName);
  }
}

Usage with redux-thunk

Some Redux middleware, such as redux-thunk, alter the store's dispatch() function, resulting in type mismatches with mapped actions in your components.

To properly type mapped actions in your components (properties whose values are set by store.mapDispatchToProps()), you can use the following type:

import { ThunkAction } from 'redux-thunk';

export type Unthunk<T> = T extends (...args: infer A) => ThunkAction<infer R, any, any, any>
  ? (...args: A) => R
  : T;

Example

// redux/user/actions.ts

import { ThunkAction } from 'redux-thunk';

export const changeName = (name: string): ThunkAction<Promise<void>, RootState, void, Action> => async (dispatch, getState) => {
  await fetch(...); // some async operation
};

In the component below, the type of this.changeName is extracted from the action type to be (name: string) => Promise<void>.

// components/my-component/my-component.tsx

import { changeName } from '../../redux/actions';

export class MyComponent {
  changeName!: Unthunk<typeof changeName>;

  componentWillLoad() {
    this.store.mapDispatchToProps(this, { changeName });
  }
}

版本与发布记录

@edsilv/stencil-redux 共发布 2 个版本,首次发布于 2019/08/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2020/05/216 年前
0.1.12019/08/217 年前

关于 @edsilv/stencil-redux 的常见问题

@edsilv/stencil-redux 是什么?

Stencil Redux - A simple redux-connector for Stencil-built web components

如何安装 @edsilv/stencil-redux?

在项目目录运行 npm install @edsilv/stencil-redux,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:redux(^4.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@edsilv/stencil-redux 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@edsilv/stencil-redux 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@edsilv/stencil-redux 可以商用吗?

@edsilv/stencil-redux 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@edsilv/stencil-redux 的下载量有多少?

根据 npm 官方下载统计,@edsilv/stencil-redux 在 2026/10/13 – 2026/11/11 这 30 天内被下载 11,531 次,日均约 384 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@edsilv/stencil-redux 还在维护吗?

最新版本 0.1.2 发布于 2020/05/21(6 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。