预期有一个赋值或函数调用,但却看到了一个带有react的表达式 no-unused-expressions

问题描述 投票:0回答:1

我正在用我的意见开发一个网站。出现这个错误,不知道如何解决

 Expected an assignment or function call and instead saw an expression  no-unused-expressions

它出现在 extraReducers 部分

我的代码:

const bookSlice = createSlice({
  name: 'book',
  initialState: { books: [], isLoading: false, error: null, book: null },
  reducers: {},
  extraReducers: (builder) => {
    //GET BOOKS
      builder.addCase(getBooks.pending, (state) => {
      state.isLoading = true;
      state.error = null;
    }),
      builder.addCase(getBooks.fulfilled, (state, action) => {
        state.isLoading = false;
        state.books = action.payload;
      }),
      builder.addCase(getBooks.rejected, (state, action) => {
        state.isLoading = false;
        state.error = action.payload;
      }),
      // INSERT BOOK
      builder.addCase(insertBook.pending, (state) => {
        state.isLoading = true;
        state.error = null;
      }),
      builder.addCase(insertBook.fulfilled, (state, action) => {
        state.isLoading = false;
        state.books.push(action.payload);
      }),
      builder.addCase(insertBook.rejected, (state, action) => {
        state.isLoading = false;
        state.error = action.payload;
      });
  },
});

如何解决该错误?

javascript reactjs redux-toolkit redux-thunk
1个回答
0
投票

这里有两种方法可以解决这个问题。

  1. 将逗号更改为分号。

    extraReducers:(构建器)=> { //获取书籍 builder.addCase(getBooks.pending, (state) => { 状态.isLoading = true; 状态.错误=空; }); builder.addCase(getBooks.fulfilled, (状态, 操作) => { 状态.isLoading = false; state.books = action.payload; }); }

  2. 使用链式

    addCase
    调用

    extraReducers:(构建器)=> { //获取书籍 建设者 .addCase(getBooks.pending, (状态) => { 状态.isLoading = true; 状态.错误=空; }) .addCase(getBooks.fulfilled, (状态, 操作) => { 状态.isLoading = false; state.books = action.payload; }); }

© www.soinside.com 2019 - 2024. All rights reserved.