【问题标题】:How to get a function with parameters in a hook with Redux?如何在 Redux 的钩子中获取带参数的函数?
【发布时间】:2020-09-13 07:45:10
【问题描述】:

我需要使用 Redux.State 通过钩子制作一个表单,我得到这样的结果:

const searchValue = useSelector(state => state.Search.searchValue);

我如何获得一个函数来传递参数并实现它?我尝试通过useDispatch(),但没有任何结果。有必要为此输入进行onChange,并且我需要获取另一个带参数的函数。

 <InputBase
  placeholder="Search…"
  value={searchValue}
  classes={{
    root: classes.inputRoot,
    input: classes.inputInput,
  }}
  inputProps={{ 'aria-label': 'search' }}
/>

【问题讨论】:

    标签: javascript redux react-redux react-hooks jsx


    【解决方案1】:
    //import your related action function in store
    import reduxStoreActionFunction from <your store path>
    
    const YourFunctionComponent = () => {
        const dispatch = useDispatch()
    
        //if you want to do it by form, you should have a onSubmitFunction to handle what should be done after pressing submit button
        const handleOnSubmit = () => dispatch(reduxStoreActionFunction(searchValue))
       return (
        <>
          <Form onSubmit={handleOnSubmit}>
            <InputBase
              placeholder="Search…"
              value={searchValue}
              classes={{
                root: classes.inputRoot,
                input: classes.inputInput,
            }}/>
            <input type="submit"/>
          </Form>
        </>
    />
       )
    
    }
    
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2019-07-05
    • 2020-04-22
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    相关资源
    最近更新 更多