React.js Why Hooks
Why Hooks?: Hooks are functions that let us “hook into” state and lifecycle functionality in function components. Hooks allow us to: reuse stateful logic between components simplify and organize our code to separate concerns, rather allowing unrelated data to get tangled up together avoid confusion around the behavior of the this keyword avoid class constructors, binding methods, and related advanced JavaScript techniques Rules for Using Hooks There are two main rules to keep in mind when using hooks: Only call hooks from React function components. Only call hooks at the top level, to be sure that hooks are called in the same order each time a component renders. Common mistakes to avoid are calling hooks inside of loops, conditions, or nested functions. useEffect(() => { if (userName !== '') { localStorage.setItem('savedUserName', userName); } }); Side Effects The primary purpose of a React component is to return some JSX to be rendered. Often, it is helpful for a ...