Category: Code

Hooks in React

Hooks help writing reusable code. useState – Function that lets you use state in a functional component. inside your functional component, deconstruct an array and pass in 2 values, first value will be a getter function and second value will be a setter, then call the useState function and pass in the initial value as …

Async await vs then

When working with API requests in React you can choose if you want to work with a promise syntax and make use of .then() or if you want to use async and await. below are examples of both: this code can be refactored by using async and await.

Fixing isseus with THIS in React

above code is broken as this is undefined in legacy react it would be solved by adding a constructor that binds this to this 🙂 Another example: When trying to hit enter, we get back “TypeError: Cannot read property ‘state’ of undefined ” the reason being that this function equals to and whenever the function …

Event handlers in React

Simple event handlers in React it makes sense to refactor the code above and make use of a single line arrow function instead when the onChange event triggers a very simple single line functionality, as below: However, we now have a UNCONTROLLED component where the value is stored in the HTML. This is not OK! …