如何在Redux的钩子中获取带有参数的函数?

时间:2020-05-26 13:29:22

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

我需要使用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' }}
/>

1 个答案:

答案 0 :(得分: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>
    </>
/>
   )

}