Skip to content

Commit

Permalink
update example with useFormContext
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 committed Jun 23, 2019
1 parent f1fb14d commit 87f270f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Expand Up @@ -16,6 +16,7 @@ you can view the source code within the folder or visit code sand box to see how
| DefaultValues | https://codesandbox.io/s/react-hook-form-defaultvalues-n5gvx |
| Default/Initial Form Value | https://codesandbox.io/s/l3mxpvmm9 |
| Dirty/Touched/Submitted | https://codesandbox.io/s/7o2wrp86k6 |
| FormContext | https://codesandbox.io/s/sad-sutherland-zvfok |
| Nested Fields | https://codesandbox.io/s/react-hook-form-nested-fields-mv1bb |
| Normalize/Format/Mask Field | https://codesandbox.io/s/387z7njwzp |
| Reset Form | https://codesandbox.io/s/jjm3wyqmjy |
Expand Down
29 changes: 29 additions & 0 deletions examples/formContext.tsx
@@ -0,0 +1,29 @@
import React from "react";
import ReactDOM from "react-dom";
import useForm, { FormContext, useFormContext } from "react-hook-form";

import "./styles.css";

function App() {
const methods = useForm();
const { register, handleSubmit } = methods;
return (
<FormContext {...methods}>
<form onSubmit={handleSubmit(data => console.log(data))}>
<label>Test</label>
<input name="test" ref={register({ required: true })} />
<label>Nested Input</label>
<Test />
<input type="submit" />
</form>
</FormContext>
);
}

function Test() {
const data = useFormContext();
return <input name="bill" ref={data.register} />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

0 comments on commit 87f270f

Please sign in to comment.