Dániel Boros
Dániel Boros

Reputation: 403

react-native, react-hook-form: FormProvider as HOC

I am new in react-hook-form, and my question, that when I wrap my whole application with FormProvider can I access the state like the react context API, or the redux provider or this component is just for inject the useform methods into the context for calling everywhere?

My case is that I have a react-native application. I wrapped my app into the FormProvider in the App.tsx. I have a form screen where I have to add some data from another screen (react-native-screens). So I tried to call getValues and setValue on the new screen but I get undefined as a result of the getValues method. Should I create custom react context with useEffect to update the form state or there is chance to solve this with useFormContext hook?

Upvotes: 0

Views: 1432

Answers (2)

marwane akef
marwane akef

Reputation: 59

Hello first you don't need react-hook-form as a global state supplier . use redux it's much simpler as to your appProvider you simply import Provider from 'react-redux' then pass the store as a prop then configure your store to take injectable states from future containers . react-hook-forms main roll is meant to handle forms : check their usage https://react-hook-form.com . i use it mainly in authentification containers to control the flow of data that is to be sent to the main store. . i hope this answer gives you some clarity .

Upvotes: 1

MHP
MHP

Reputation: 663

If I understood correctly ,in order to accessing your states anywhere in functional components by redux you are able to use useSelector for getting data and useDispatch for dispatching data. Also in the react context you capable of to creating your custom hook for easier accessing to your states by useContext or directly use that and for setting your states you can use useReducer (better way) or set in the context.

React hooks

Redux hooks

Upvotes: 0

Related Questions