Skip to content

Commit

Permalink
placing data in context - createContext
Browse files Browse the repository at this point in the history
  • Loading branch information
jessilyneh committed Mar 4, 2022
1 parent 29fe9ca commit 10de4e8
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
import React from "react";
import React, { createContext } from "react";
import ReactDOM from "react-dom";
import "./index.css";
import { useInput } from "./useInput";

// useInput will received a value (nameProps)
// and atribute the initial value (resetName)
export default function App() {
const [nameProps, resetName] = useInput("");
const [birthProps, resetBirth] = useInput("");

const submit = (e) => {
e.preventDefault();
alert(`${nameProps.value} was born in ${birthProps.value}`);
resetBirth();
resetName();
};

// just need pass nameProps to value ({value,
// onChange}, resetValue) inside {...nameProps}
export const TreesContext = createContext();

const trees = [
{ id: "1", type: "migraines" },
{ id: "2", type: "digestive problem" },
{ id: "3", type: "sinus headaches" },
{ id: "4", type: "tension" }
];
export default function App() {
return (
<form onSubmit={submit}>
<input {...nameProps} type="text" placeholder="name..." />

<input {...birthProps} type="date" />

<button>Submit</button>
</form>
<div>
<h1>Types of headaches</h1>
</div>
);
}

ReactDOM.render(
<React.StrictMode>
<TreesContext.Provider value={{ trees }}>
<App />
</React.StrictMode>,
</TreesContext.Provider>,
document.getElementById("root")
);

0 comments on commit 10de4e8

Please sign in to comment.