DEV Community

Dillon
Dillon

Posted on

Getting Hooked on React

Hooks are special functions. Hooks CAN ONLY be called inside the body of a function component.

Rules of Hooks:

Thou shalt call the Hook within the scope of a react application.
Thou shalt call your hooks from the toop level of the component. (this error would call if you were to try to call your hook in a conditional. it doesn't like this because calling it within a conditional means that you could simply not call it in a specific case.)

useState - enables us to hook into the component's state
useId - enables us to store a unique identifier on the component instance

You should not mess with or try to update state.

In javascript, you could do this, but it is a big "no-no" in react.

React.useRef(initial value) -- if you leave this blank, it is undefined.

React states shouldn't be mutated, react refs are meant to be mutated.

Refs are an object to what is being referred. So, if you are calling a funciton using useRef, you have to slide in the refName.current.value to get to the value you want. Otherwise, you aren't getting past the {current: "whatever"}

Top comments (0)