react custom hook lifecycle code example
Example 1: react custom hooks
import React, { useState } from "react";
const useForm = callback => {
const [values, setValues] = useState({});
return {
values,
onChange: e => {
setValues({
...values,
[e.target.name]: e.target.value
});
},
onSubmit: e => {
e.preventDefault();
callback();
}
};
};
export default function App() {
const { values, onChange, onSubmit } = useForm(() => {
console.log(values.username);
console.log(values.email);
});
return (
<div>
<form onSubmit={onSubmit}>
<input type="text" name="username" onChange={onChange} />
<input type="email" name="email" onChange={onChange} />
<input type="submit" value="Sing-in" />
</form>
</div>
);
}
Example 2: reference hook in react
import React, {useRef} from 'react'
const displayNode = useRef(null)
displayNode.current.textContent = `${count} Appointments Successfully Uploaded`
<div ref={displayNode} className="text-center mt-3 msg" style={{ color: "green", fontWeight: "800" }}></div>