Categories
React Answers

How to manually invoke Link with React Router v6?

Sometimes, we want to manually invoke Link with React Router v6.

In this article, we’ll look at how to manually invoke Link with React Router v6.

How to manually invoke Link with React Router v6?

To manually invoke Link with React Router v6, we can use the navigate function.

For instance, we write

import React, { useCallback } from "react";
import { useNavigate } from "react-router-dom";

export default function comp() {
  const navigate = useNavigate();
  const handleOnClick = useCallback(
    () => navigate("/example", { replace: true }),
    [navigate]
  );

  return (
    <button type="button" onClick={handleOnClick}>
      Go home
    </button>
  );
}

to create the handleOnClick function with the useCallback hook called with a function that calls navigate with the '/example' destination route.

replace is set to true to replace the existing entry in the browser history.

We get the navigate function from the useNavigate hook.

Then we set the onClick prop of the button to handleOnClick to call it to navigate to /example when we click on the button.

Conclusion

To manually invoke Link with React Router v6, we can use the navigate function.

Categories
React Answers

How to do API call in React?

Sometimes, we want to do API call in React.

In this article, we’ll look at how to do API call in React.

How to do API call in React?

To do API call in React, we call the useEffect hook.

For instance, we write

import React, { useState, useEffect } from "react";

const Comp = () => {
  //...
  useEffect(() => {
    fetchDataFromBackend();
  }, []);
  //...
};

to call useEffect in the Comp component with a callback that calls fetchDataFromBackend to make the request to the API we want.

We call useEffect with an empty array so that the callback only runs when the component mounts.

Conclusion

To do API call in React, we call the useEffect hook.

Categories
React Answers

How to loop and render elements in React without an array of objects to map?

Sometimes, we want to loop and render elements in React without an array of objects to map.

In this article, we’ll look at how to loop and render elements in React without an array of objects to map.

How to loop and render elements in React without an array of objects to map?

To loop and render elements in React without an array of objects to map, we create an array.

For instance, we write

const Comp = ({ level }) => {
  //...
  return Array.from({ length: level }, (item, index) => (
    <span className="indent" key={index}></span>
  ));
};

to call Array.from with { length: level } to create an array with the length set to level.

Then we call it with a callback that maps the empty array entries to spans.

Conclusion

To loop and render elements in React without an array of objects to map, we create an array.

Categories
React Answers

How to tell the version of React running at runtime in the browser?

Sometimes, we want to tell the version of React running at runtime in the browser.

In this article, we’ll look at how to tell the version of React running at runtime in the browser.

How to tell the version of React running at runtime in the browser?

To tell the version of React running at runtime in the browser, we can use the React.version property.

For instance, we write

import React from "react";

console.log(React.version);

to log the React version by calling console.log with React.version.

Conclusion

To tell the version of React running at runtime in the browser, we can use the React.version property.

Categories
React Answers

How to update React component every second with JavaScript?

Sometimes, we want to update React component every second with JavaScript.

In this article, we’ll look at how to update React component every second with JavaScript.

How to update React component every second with JavaScript?

To update React component every second with JavaScript, we can use the setInterval function to update a state in the useEffect hook.

For instance, we write

const [time, setTime] = useState(Date.now());

useEffect(() => {
  const interval = setInterval(() => setTime(Date.now()), 1000);
  return () => {
    clearInterval(interval);
  };
}, []);

in a function component.

We get set the initial value of time to the timestamp returned by Date.now.

Then in the useEffect callback, we call setInterval with a callback that calls setTime with Date.now() to update the time to the current timestamp.

And we call it with 1000 to do that every second.

We return a callback that calls clearInterval with interval to clear the timer when we unmount our component.

The useEffect callback runs once when we mount our component since we called it with an empty array.

Conclusion

To update React component every second with JavaScript, we can use the setInterval function to update a state in the useEffect hook.