javascript - 使用 typescript react redux 设置 reducer

标签 javascript reactjs typescript react-redux

我正在使用 redux 设置 ts 并遇到了很多问题 - 主要是由于我缺乏知识,但在网上找不到太多。我看到的错误如下:

Operator '+' cannot be applied to types 'CounterState' and '1'.

我的代码如下:

interface CounterState {
  state: number;
}

const initialState: CounterState = {
  state: 0
}

interface Action {
  type: string;
}

export const counterReducer = (state = initialState, action: Action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};

如果我对此进行更新,它会起作用,但似乎我不必为状态定义类型。以下作品

const initialState = 0;
interface Action {
  type: string;
}

export const counterReducer = (state = initialState, action: Action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};

最佳答案

最好始终强类型化你的 reducer,包括状态和 Action 。

这里我展示了一个正确定义的 reducer 和 store 看起来如何在一起的例子。 希望这个例子连同我的评论对你有所帮助。

import { Reducer } from "redux"; //This is just a Type we import from Redux.

interface IncrementAction {
  type: "INCREMENT"; //Define your action names
}

interface DecrementAction {
  type: "DECREMENT"; //Define your action names
}

type PossibleCounterActions = IncrementAction | DecrementAction; 
// The actions could/should be defined in another file for clarity


type CounterState = number;

const defaultState = 0;

// We bind the variable counterReducer to the Reducer type taken from redux.
// The our reducer code gets cleaner and we know the return type and arguments.
const counterReducer: Reducer<CounterState, PossibleCounterActions> = (state = defaultState, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
}



// PS. This is not part of the question but it's
// a nice side-effect you can do when you have properly defined reducers.
// In the file where you create your store you can now get your store
// interface from the returntype of the redcuers using this pattern. 
const reducers = {
  counter: counterReducer
};

// Now we can get the entire store state from the declaration in the reducers.
export type IStoreState = { [k in keyof (typeof reducers)]: ReturnType<(typeof reducers)[k]> };

//More code to initialize your store.....

关于javascript - 使用 typescript react redux 设置 reducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757383/

相关文章:

typescript - 在 Angular 2 observable 中使用 RxJS 订阅运算符

javascript - 如何测试 React 组件是否已经从 switch 状态渲染?

typescript - 基于 Vue 类的组件警告 : Property is not defined on the instance but referenced during render

javascript - 直接从 &lt;script&gt; HTML 标记将变量传递给 Javascript

javascript - 当按下向上箭头键时,将文本光标发送到文本框焦点的末尾

javascript - 增加 map 容器大小的宽度而不移动 map

javascript - 使用 angularJs ng-Pattern 进行 10 位正则表达式验证

javascript - 我如何 'swap' 对我的应用程序中的 react /插件使用react?

javascript - "Cannot read property ' findDOMNode ' of undefined"使用 React-widgets 时

javascript - Angular 4 - 使用 NG-IF 时出现 "Expression has changed after it was checked"错误