如何连接redux商店和输入值?
请指教。我想将我的输入值与组件中的商店和值输入连接。 甚至可以做到这一点
我该怎么做,当您进入输入时,现在
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请检查是否将
InputValue
缩小器注册到商店。然后在
搜索
组件中,将inputslice
替换为inputValue
(因为您在combinereducers中注册为“ InputValue”,
),,像这样Please check if you register the
inputValue
reducer to the store like this.Then in the
Search
component, replaceinputSlice
intoinputValue
(because you registered as "inputValue" incombineReducers
), like this