23
loading...
This website collects cookies to deliver better user experience
app.js
I’ve set two input numbers and one button alongside the Calculator
component. We set the numbers on the inputs and when we click the button the states get updated and the component gets the number via props. Nothing fancy.import { useRef, useState } from "react";
import "./App.css";
import Calculator from "./Calculator";
const App = () => {
const inputA = useRef(0);
const inputB = useRef(0);
const [numbers, setNumbers] = useState({
numberA: inputA.current.value,
numberB: inputB.current.value,
});
const getTotalHandler = () => {
setNumbers({
numberA: +inputA.current.value,
numberB: +inputB.current.value,
});
};
return (
<>
<div className="container">
<div>
<label>Input A</label>
<input type="number" ref={inputA} />
</div>
<div>
<label>Input B</label>
<input type="number" ref={inputB} />
</div>
<button onClick={getTotalHandler}>Calculate</button>
<Calculator numberA={numbers.numberA} numberB={numbers.numberB} />
</div>
</>
);
};
export default App;
Calculator
that receives via props numberA
and numberB
and returns the sum.const Calculator = ({ numberA, numberB }) => {
return <h1>The total is {numberA + numberB}</h1>;
};
export default Calculator;
useEffect
to execute a function that logs me when the component it’s mounted. First I import the useEffect
hook from react
.import { useEffect } from "react";
import { useEffect } from "react";
const Calculator = ({ numberA, numberB }) => {
useEffect(() => {
console.log(`First render`);
}, []);
return <h1>The total is {numberA + numberB}</h1>;
};
export default Calculator;
App
component state and update the Calculator
props this log function will not execute again. Let’s check:numberA
and numberB
props but the function did not execute. import { useEffect } from "react";
const Calculator = ({ numberA, numberB }) => {
useEffect(() => {
console.log(`First render`);
}, []);
useEffect(() => {
console.log(`This gets executed each time the props are updated`);
}, [numberA, numberB]);
return <h1>The total is {numberA + numberB}</h1>;
};
export default Calculator;
useEffect
hooks get executed.useEffect
hook to get data from an API each time the props of our component is updated. Rick
that uses an id received via props to get data from the Rick and Morty public api.import { useState, useEffect } from "react";
const Rick = ({ id }) => {
const [character, setCharacter] = useState(null);
const [request, setRequest] = useState(`pendent`);
useEffect(() => {
setRequest(`pendent`);
const getApiResults = (characterId) => {
fetch(`https://rickandmortyapi.com/api/character/${characterId}`)
.then((response) => response.json())
.then((data) => {
setCharacter(data);
setRequest(`completed`);
});
};
getApiResults(id);
}, [id]);
let content = ``;
if (request === `completed`) {
content = (
<>
<img src={character.image} alt={character.name} />
<h1>{character.name}</h1>
</>
);
}
return content;
};
export default Rick;
app
component I have a button that generates a random number and passes it via props to the Rick
component. On the first render we just have the button: