【问题标题】:Custom ESLint rule for Redux reducersRedux reducer 的自定义 ESLint 规则
【发布时间】:2021-01-14 15:47:28
【问题描述】:

我将 Redux 和 Redux Toolkit 与 ESLint 结合使用。

当我实现 extraReducers 时,有时我不需要 Redux 提供的 stateaction 属性,这反过来又会使 ESLint 抛出错误。

现在,我正在使用以下注释手动禁用该特定行的 linter 规则:

// eslint-disable-next-line no-unused-vars

但是,有没有办法重写函数声明以避免 linter 规则,或者我可以仅针对这种情况全局禁用该规则?

const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {},
  extraReducers: {
    // eslint-disable-next-line no-unused-vars
    [fetch.pending]: (state, action) => {
      state.status = "loading"
    },
  },
})

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    在这种情况下,您可以省略 action 参数。但是如果未使用的参数在使用的参数之前,那么一种选择是使用underscore syntax,这是很常见的做法:

    const example = (_foo, bar) => console.log(bar);

    【讨论】:

      【解决方案2】:

      您只是不需要声明不使用的参数。您可以随时添加它们。

      const userSlice = createSlice({
        name: "user",
        initialState,
        reducers: {},
        extraReducers: {
          [fetch.pending]: () => {
            state.status = "loading"
          },
        },
      })
      

      也很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-29
        • 2019-12-21
        • 2016-02-03
        • 2019-02-03
        • 2017-12-10
        • 2019-07-15
        • 2022-01-01
        • 2020-06-30
        相关资源
        最近更新 更多