在Redux工具包中导入还原器如何工作?
因此,我正在阅读有关 rtk
的文档,所示的示例是在使用此代码创建切片之后:
import { createSlice } from '@reduxjs/toolkit'
const initialState = {
value: 0,
}
export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the Immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1
},
decrement: (state) => {
state.value -= 1
},
incrementByAmount: (state, action) => {
state.value += action.payload
},
},
})
// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer
然后在商店中导入还原器:
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from '../features/counter/counterSlice'
export const store = configureStore({
reducer: {
counter: counterReducer,
},
})
但是我从<<<代码>导入是它导入反向
,尽管在切片文件中没有反向
导出,但Redux Toolkit功能或React功能或A React功能或一个我不知道的JavaScript功能?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是“默认导出”和“默认导入”。
您注意到所有出口都有一个名称 - 除了
在另一个文件中,您可以看到两个不同的符号:
It's a "default export" and "default import".
You notice that all exports have a name - except for
In the other file, you can see two different notations:
这是ES模块功能,因此是JavaScript的事情。当您用
默认
关键字导出某些内容时,可以使用所需的任何名称导入它,而没有{}
。在slice文件中,有一个导出:您可以用所需的名称导入它,他们选择将其称为
underducer
:它在语句或 javascript模块 mdn上。
nequ name enfort
和<代码>默认导出。要了解更多信息,请访问 extortIt's an ES Modules feature, so it's a JavaScript thing. When you export something with
default
keyword, you can import it with whatever name you want and without{}
. In slice file, there is this export:And that you can import it with the name you want, they choose to call it
counterReducer
:It's the different between
named exports
anddefault exports
. To know more about it, visit the doc for export statement or JavaScript modules on MDN.