react native hook subscribe code example
Example 1: useEffect
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
You clicked {count} times
);
}
Example 2: react custom hooks
import React, { useState } from "react";
// custom hooks useForm
const useForm = callback => {
const [values, setValues] = useState({});
return {
values,
onChange: e => {
setValues({
...values,
[e.target.name]: e.target.value
});
},
onSubmit: e => {
e.preventDefault();
callback();
}
};
};
// app component
export default function App() {
const { values, onChange, onSubmit } = useForm(() => {
console.log(values.username);
console.log(values.email);
});
return (
);
}