我正在用我的意见开发一个网站。出现这个错误,不知道如何解决
Expected an assignment or function call and instead saw an expression no-unused-expressions
我的代码:
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;
});
},
});
如何解决该错误?
这里有两种方法可以解决这个问题。
将逗号更改为分号。
extraReducers:(构建器)=> { //获取书籍 builder.addCase(getBooks.pending, (state) => { 状态.isLoading = true; 状态.错误=空; }); builder.addCase(getBooks.fulfilled, (状态, 操作) => { 状态.isLoading = false; state.books = action.payload; }); }
使用链式
addCase
调用
extraReducers:(构建器)=> { //获取书籍 建设者 .addCase(getBooks.pending, (状态) => { 状态.isLoading = true; 状态.错误=空; }) .addCase(getBooks.fulfilled, (状态, 操作) => { 状态.isLoading = false; state.books = action.payload; }); }