react hooks return js code 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: components react to hooks
import React, { useState } from 'react';
function Example() {
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}