Categories
React Answers

How to fix this.setState isn’t merging states as expected with React?

Sometimes, we want to fix this.setState isn’t merging states as expected with React.

In this article, we’ll look at how to fix this.setState isn’t merging states as expected with React.

How to fix this.setState isn’t merging states as expected with React?

To fix this.setState isn’t merging states as expected with React, we make a copy of the original state object, modify it, and then call setState with the modified copied object.

For instance, we write

const { selected: _selected } = this.state;
const selected = { ..._selected, name: "Barfoo" };
this.setState({ selected });

to copy the _selected state with ....

Then we add the name property to it.

Then we assign that to selected and call setState with { selected } to update it.

Conclusion

To fix this.setState isn’t merging states as expected with React, we make a copy of the original state object, modify it, and then call setState with the modified copied object.

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.