在Redux工具包中导入还原器如何工作?

发布于 2025-02-13 22:19:49 字数 1322 浏览 1 评论 0 原文

因此,我正在阅读有关 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功能?

So I'm reading through the docs for rtk , the example shown is after creating a slice with this code:

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

and then importing the reducer in the store like this :

import { configureStore } from '@reduxjs/toolkit'
import counterReducer from '../features/counter/counterSlice'

export const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
})

But what I'm seeing from the import is that it's importing counterReducer although there's no counterReducer export in the slice file , is that a Redux Toolkit feature or a React feature or a JavaScript feature that I'm not aware of?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2025-02-20 22:19:50

这是“默认导出”和“默认导入”。

您注意到所有出口都有一个名称 - 除了

export default counterSlice.reducer

在另一个文件中,您可以看到两个不同的符号:

// has braces - imports the named export `configureStore` from `@reduxjs/toolkit`
import { configureStore } from '@reduxjs/toolkit'
// has no braces - imported the default export from the file `../features/counter/counterSlice` and gives it the name `counterReducer` for this file.
import counterReducer from '../features/counter/counterSlice'

It's a "default export" and "default import".

You notice that all exports have a name - except for

export default counterSlice.reducer

In the other file, you can see two different notations:

// has braces - imports the named export `configureStore` from `@reduxjs/toolkit`
import { configureStore } from '@reduxjs/toolkit'
// has no braces - imported the default export from the file `../features/counter/counterSlice` and gives it the name `counterReducer` for this file.
import counterReducer from '../features/counter/counterSlice'
Smile简单爱 2025-02-20 22:19:50

这是ES模块功能,因此是JavaScript的事情。当您用默认关键字导出某些内容时,可以使用所需的任何名称导入它,而没有 {} 。在slice文件中,有一个导出:

export default counterSlice.reducer

您可以用所需的名称导入它,他们选择将其称为 underducer

import counterReducer from '../features/counter/counterSlice'

它在 nequ name enfort 和<代码>默认导出。要了解更多信息,请访问 extort 语句或 javascript模块 mdn上。

It'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:

export default counterSlice.reducer

And that you can import it with the name you want, they choose to call it counterReducer:

import counterReducer from '../features/counter/counterSlice'

It's the different between named exports and default exports. To know more about it, visit the doc for export statement or JavaScript modules on MDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文