Skip to content

Instantly share code, notes, and snippets.

@arunkumar413
Created April 15, 2020 06:56
Show Gist options
  • Save arunkumar413/3a898aaadea07c8e204cf06e1d1858b9 to your computer and use it in GitHub Desktop.
Save arunkumar413/3a898aaadea07c8e204cf06e1d1858b9 to your computer and use it in GitHub Desktop.
Lifting reactjs state using hooks
import React, { useState } from "react";
export default function Child(props) {
const handleChange = event => {
props.onChange(event.target.value);
};
return (
<div>
<input value={props.value} onChange={handleChange} />
</div>
);
}
import React, { useState, useEffect } from "react";
import Child from "./Child";
export default function Parent(props) {
const [value, setValue] = useState("");
const handleChange = newVal => {
setValue(newVal);
};
return (
<div className="App">
<h2> {value} </h2>
<Child value={value} onChange={handleChange} />;
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment