fundamentals-in-react

Fundamentals of React

In this post I am going to explain fundamentals in React

React is not a Framework.

React is declarative

JSX IN REACT

  • It performs optimizations while translating to regular JavaScript.
  • It makes it easier for us to create templates.
  • Instead of separating the markup and logic in separated files, React uses components for this purpose.

Components

 components are the building blocks of all the React apps, from the basics to more complex applications that will compose of one to many components.

Think of it in Javascript as the class or the functions that can accept inputs and returns elements based on how the UI should look like.

There are 2 different components to React, and these are:

  • Class Components — is a stateful component. Class is one of the most common ways to create a component because it gives a wider range of functionalities and more control especially when it comes to using lifecycle methods.
  • Functional Components — the simplest and the most basic component is the functional component that can be written into 2 ways, you can use the old way or simply use ES6’s arrow function syntax. Functional component is stateless.

Virtual DOM

HOOKS

State

Props

 useState

This is used specifically on Functional component, functional component as I have mentioned is stateless, and by using useState, functional components can become stateful.

useEffect

The useEffect also specifically made for Functional components, it helps functional components make use of the lifecycle hooks that were only available for class components before.

setState

This is probably the only valid way or the best way to update the state of your application.

Component Lifecycle Methods

 Every react component has its lifecycle. There are several lifecycle methods that you need to fully understand, read about them from documentations and find tutorials, remember to understand how they work, how they function, which one to use, and most importantly know their timings.

  • componentWillMount()
  • componentDidMount()
  • componentWillReceiveProps()
  • shouldComponentUpdate()
  • componentWillUpdate()
  • componentDidUpdate()

Lists and keys 

List and keys are  the basics that should be doing should  that include React CRUD.

Advanced ReactJS

  1. Context

This is where the passing of data comes in place into different components and this where Redux comes into play. Here is when we can create global variables so you  can easily skip the component tree when you  are passing props you  don’t have to pass it manually on every level, it is like jumping off from Grandparent components to child component.

2. Higher-Order Components

This is an advanced trick in React about reusing component logic, this happens when a component function which you  will then call a higher-order component takes a component and returns a new component.

3. Refs

Refs lets you access the DOM. One of the most important rules in React is to not imperatively manipulate the DOM, however, there are instances wherein you  have to, so here comes ‘refs’ to the rescue.

4. Error Boundaries

This helps catch errors to be able to properly setup UI fallback if there are any exceptions, because of React’s different way of handling the DOM, the catching of errors will also be handled differently.

5. Portals 

Portals help you render UI that is outside the root element of your React app. The best way to imagine this is to think of a doorway that will bring you to magical places in different locations or countries of your like.

6. HTTP Requests 

Here comes API. You can either use fetch or a tool like Axios to help handle GET and POST requests.

Conclusion

In this post, I have explained, fundamentals in ReactJS, what are components, Virtual DOM, Hooks, jsx, State, Props and how and where to use them in  React.

In my next post I am going to describe Getting started with React

This post is part of React-Step by step.

Back to home page

Leave a Reply

Your email address will not be published. Required fields are marked *