如何连接redux商店和输入值?

发布于 2025-02-08 08:00:15 字数 1705 浏览 1 评论 0原文

请指教。我想将我的输入值与组件中的商店和值输入连接。 甚至可以做到这一点

我该怎么做,当您进入输入时,现在

 const Search: React.FC = () => {
      const dispatch = useAppDispatch()
      const inputValue = useAppSelector((state) => state.inputSlice.inputValue)
      const changeInputValue = debounce(
        (e: React.ChangeEvent<HTMLInputElement>) => {
          dispatch(inputChangeValue(e.target.value))
        },
        200
      )

  return (
    <div className={styles.search__wrapp}>
      <input
        value={inputValue}
        onChange={(e) => {
          changeInputValue(e)
        }}
        placeholder="Find your favorite character"
        className={styles.search}
      />
      <button className={styles.button__search}>
        <img src={search} alt="search icon" />
      </button>
      <button
        onClick={() => {
          dispatch(removeInputField())
        }}
        className={styles.clearInput}
      >
        <img src={closeIcon} alt="inputClear icon" />
      </button>
    </div>
  )
}

,什么也不会发生(字符串总是空的)我的Redux Toolkit Slice

interface Input {
  inputValue: string
}

const initialState: Input = {
  inputValue: '',
}

export const inputSlice = createSlice({
  name: 'inputValue',
  initialState,
  reducers: {
    inputChangeValue: (state, action: PayloadAction<string>) => {
      state.inputValue = action.payload
    },
    removeInputField: (state) => {
      state.inputValue = ''
    },
  },
})

export const { inputChangeValue, removeInputField } = inputSlice.actions

export const selectCount = (state: RootState) => state.inputSlice.inputValue

please advise. I want to connect my input value on store and value input in component. How can I do this and is it even possible to do this

Now when you enter into the input, nothing happens (the string is always empty)

 const Search: React.FC = () => {
      const dispatch = useAppDispatch()
      const inputValue = useAppSelector((state) => state.inputSlice.inputValue)
      const changeInputValue = debounce(
        (e: React.ChangeEvent<HTMLInputElement>) => {
          dispatch(inputChangeValue(e.target.value))
        },
        200
      )

  return (
    <div className={styles.search__wrapp}>
      <input
        value={inputValue}
        onChange={(e) => {
          changeInputValue(e)
        }}
        placeholder="Find your favorite character"
        className={styles.search}
      />
      <button className={styles.button__search}>
        <img src={search} alt="search icon" />
      </button>
      <button
        onClick={() => {
          dispatch(removeInputField())
        }}
        className={styles.clearInput}
      >
        <img src={closeIcon} alt="inputClear icon" />
      </button>
    </div>
  )
}

my redux toolkit slice

interface Input {
  inputValue: string
}

const initialState: Input = {
  inputValue: '',
}

export const inputSlice = createSlice({
  name: 'inputValue',
  initialState,
  reducers: {
    inputChangeValue: (state, action: PayloadAction<string>) => {
      state.inputValue = action.payload
    },
    removeInputField: (state) => {
      state.inputValue = ''
    },
  },
})

export const { inputChangeValue, removeInputField } = inputSlice.actions

export const selectCount = (state: RootState) => state.inputSlice.inputValue

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

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

发布评论

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

评论(1

流年已逝 2025-02-15 08:00:15

请检查是否将InputValue缩小器注册到商店。

import { combineReducers, configureStore } from "@reduxjs/toolkit";
const combinedReducers = combineReducers({
   inputValue: inputSlice.reducer
});

const store = configureStore({
    reducer: (state, action) => combinedReducers(state, action)
});
export default store;

然后在搜索组件中,将inputslice替换为inputValue(因为您在combinereducers中注册为“ InputValue”,),,像这样

const inputValue = useAppSelector((state) => state.inputValue.inputValue)

Please check if you register the inputValue reducer to the store like this.

import { combineReducers, configureStore } from "@reduxjs/toolkit";
const combinedReducers = combineReducers({
   inputValue: inputSlice.reducer
});

const store = configureStore({
    reducer: (state, action) => combinedReducers(state, action)
});
export default store;

Then in the Search component, replace inputSlice into inputValue (because you registered as "inputValue" in combineReducers), like this

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