REACT 1, React props, event handlers, basic useState(), useEffect(), React forms/inputs and Class components

Node.js is a javascript environment, we use npm (Node Package Manager) to install modules and use a packages.json file to track them.

To create a React application folder we run:

npx create-react-app (name of the app)

//its /public folder contains the app root
//its /src folder has the different page components

React is a javascript library used for user interface, based on single-role components.

We render DOM elements in the React components by abstracting HTML tags using jsx.

chevron-rightRender React DOM elements guidehashtag

With Javascript, we needed to createElement, content, a selector, and then render:

<script type="text/javascript">
  var divNode = document.createElement('div');
  divNode.innerText = 'Hello World';

  var rootElement = document.querySelector('#root');
  rootElement.appendChild(divNode);
</script>

On React we can:

const element = React.createElement("div", {
  children: "Hello World",
});

const rootElement = document.querySelector("#root");
ReactDOM.render(element, rootElement);

JSX is syntax sugar used to shortcut the createElement:

const element = <div>Hello World</div>;

const rootElement = document.querySelector("#root");
ReactDOM.render(element, rootElement);

Then we implement it on components:

import React from "react";
import ReactDOM from "react-dom";

function HelloWorld() {
  return <div>Hello World</div>;
}

export default Base

//we will use the root in App.js
const root = ReactDOM.createRoot(document.getElementById('root'));

We can interdent style properties by using an array of strings.

/*The element position depends on the index used*/
<div className='position-relative'>
  <h1 className='position-absolute' style={{ [["left", "right"][0]]: "0%" }}>
    Left element
  </h1>

  <h1 className='position-absolute' style={{ [["left", "right"][1]]: "0%" }}>
    Right element
  </h1>
</div>

A component needs to import React and can reference multiple components, called child components, but can export only one.

rendered React component

We use CamelCase for component names to differentiate from default HTML tags.

We use javascript expressions and {} to render logic and variables on JSX.

chevron-rightJavascript expressions on React Componentshashtag

We set the javascript operations before the return of the component.

We can invoke functions and render loops:

React Props, event handlers, and useState

Props are arguments passed into React components, they are read-only and can't be updated.

chevron-rightProps used with javascript expressionhashtag

We can edit the props property using javascript expressions:

to then assign the props values on the rendering components:

We can add Event Handlers to React components, like onClick(), onChange() or onSubmit():

We can pass useState() and functions using props:

chevron-rightuseState() and functions on propshashtag

We use the useEffect() hook to set-up a setInterval() based on isActive and isPaused useState().

Then we pass useState() and the functions to the <Tempo> prop

We can then use the prop function and the prop useState() in the imported component:

After the useEffect() setInterval() is started we can render its useState() using another component.

We use the React Hook useState to track the state of a component and update it:

chevron-rightUpdating Reactjs useState() hashtag

We use destructuring on the imported useState object, into a current state variable, and a set function to update it.

useState() after onClick()

We can pass the useState() and its setter function as props:

React useEffect with setInterval()

We use the useEffect(function, dependency) hook to synchronize a component with an external variable.

When a dependency state changes the useEffect renders:

chevron-rightuseEffect with dependency useStatehashtag

We need 2 states, one for the dependency change and the other for the javascript expression.

useEffect function rendering after onClick state

We can implement javascript expression using ternary-operators for DOM content:

We can also use && to render React components:

We can use a useEffect() hook to start/pause a setInterval(), to do so we need 2 useState() dependencies.

One for the onClick() starter event and another for the useState() time-limit we want to use to stop it

useEffect dependency used with setInterval()

We can also trigger setInterval() and clearInterval() using the same dependency in the useEffect condition:

chevron-rightReset/Restart single button with useEffecthashtag

The stop/pause button will reset the current seconds but not the setInterval(), for that we use the reset button.

We use useEffect() conditionals to trigger the setInterval().

We reset(seconds), and we clearInterval() but we also re-start, by using setIsActive(!isActive) to trigger the useEffect() conditional again while keeping the gradi counter.

We stop seconds and active using reset().

start/reset/re-start on single button

Including a function inside useEffect() dependencies will Error: This dependency changes on every render.

We clean-up event at the end of useEffect() to avoid memory leaks; events can still be running when the component unmounts, this avoid re-remders.

We use the React Hook useCallback() to call the function only when Its dependency changes.

To implement font-awesome in Reactjs we:

To deploy a Reactjs app GitHub repository on Netlify arrow-up-rightwe edit the package.json:

ReactJs forms, inputs, and submit

We render input controlled-components using useState() and callback functions.

chevron-rightComplete form, input and submit ReactJShashtag

On the DOM, we add the onSubmit() attribute to the <form> component.

We use target.name to select which useState() gets which target.value to render.

While onSubmit() we print the current useStates().

fotm, input and submit with rendered values

We can also render uncontrolled input components, using the useRef hook, we set the callback function and the useState outside of the input component

Class component, useState() and fetch()

Apart from function components, there are also Class components.

We need the component React hook and the render(){} method for the DOM.

chevron-rightClass components with useState(), setState() and external componenthashtag

We can't use the useState() hook in a class component, we can initialize multiple useState() in the state object.

We use a this.method with a callback function to setState().

We can use external functions with a toggle operator.

Class component useState()
chevron-rightClass component Fetch setState()hashtag

We use componentDidMount() to render after a fetch().

To catch a fetch() error we add an extra then() and useState().

Class component error catch, fetch, and isloading

Last updated