What are hooks and why use them?
As I mentioned before, stateless components are generally easier to write and test. They should be the go-to component of ReactJS developers, but they were often overlooked because they could not manage state. At the beginning of 2019, the ReactJS team added hooks to the library. Hooks add state functionality to stateless components (therefore, it is better to only use the term functional components). One specific hook called useState
is a function that returns a stateful value and a function to update it. You may recognize it from our previous section about state in React components.
Let’s go back to our example of a stateful component, change it to a functional one, and add the useState
hook, as follows:
import React, {useState} from "react"; import {Text} from "react-native"; const Welcome = () => { const [name, setName] = useState('World!'); return <Text>Hello, {name}<...