简体   繁体   English

如何从 react-hook-form + Redux 调度

[英]How to dispatch from react-hook-form + Redux

I'm using react-hook-form ( https://react-hook-form.com/ ).我正在使用 react-hook-form ( https://react-hook-form.com/ )。 I would like to dispatch action from inside of react-hook-form.我想从 react-hook-form 内部发送动作。

In following case, I am supposed to dispatch using props.dispatch , when onSubmit is fired.在以下情况下,当 onSubmit 被触发时,我应该使用props.dispatch进行调度。

However I could not figure out how to dispatch and update state by setState.但是我不知道如何通过 setState 调度和更新状态。

import React from 'react'
import useForm from 'react-hook-form'

export default function App(props) {
  const { register, handleSubmit, watch, errors } = useForm()
  const onSubmit = data => { 
           console.log(data);
           props.dispatch({type: 'CONFIRM'}); //--> Worked!!  
        }

  console.log(watch('example')) // watch input value by passing the name of it

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input name="example" defaultValue="test" ref={register} />
      <input name="exampleRequired" ref={register({ required: true })} />      
      <input type="submit" />
    </form>
  )
}

Does anyone give me an advice?有人给我建议吗?

class Sample extends Component {
    constructor(props){
        super(props);
        this.state = {
            mode: 'input'
        }
    }

    render() {
        switch (this.state.mode) {
            case 'input':
                return (<App />);
            case 'confirm':
                return (<App01 />);
            default:
                return (<App02 />);
        }
    }
}

export default connect((state)=>state)(Sample);

Using dispatch with redux has nothing to do with react-hook-form library.在 redux 中使用 dispatch 与react-hook-form库无关。

If you using redux-hooks , which is an alternative for connect and a better approach if you using hooks anyway with react-hook-form :如果您使用redux-hooks ,这是connect的替代方法,如果您无论如何都使用 hooks 和react-hook-form ,这是一种更好的方法:

React's new "hooks" APIs give function components the ability to use local component state, execute side effects, and more. React 的新“钩子”API 使函数组件能够使用本地组件状态、执行副作用等。

React Redux now offers a set of hook APIs as an alternative to the existing connect() Higher Order Component. React Redux 现在提供了一组钩子 API 作为现有 connect() 高阶组件的替代方案。 These APIs allow you to subscribe to the Redux store and dispatch actions, without having to wrap your components in connect().这些 API 允许您订阅 Redux 存储和分派操作,而无需将组件包装在 connect() 中。

import { useDispatch } from 'react-redux';
function App() {
  const dispatch = useDispatch();
  // use distpach
}

If you using connect :如果您使用connect

import { connect } from 'react-redux';
function App({ dispatch }) {
  // use dispatch
}

export default connect()(App);

I figured out.我想通了。 I am using combineReducers .我正在使用combineReducers It is distracted.它分心了。 I should have written like following.我应该像下面这样写。

combineReducers({
    sample01: sample01Reducer, sample02: sample02Reducer,    //Reducers
class Sample extends Component {
    constructor(props){
        super(props);
        this.state = {
            mode: 'input'
        }
    }

    render() {
        switch (this.props.sample01.mode) {
            case 'input':
                return (<App />);
            case 'confirm':
                return (<App01 />);
            default:
                return (<App02 />);
        }
    }
}

export default connect((state)=>state)(Sample);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM