Categories
React Answers

How to check if the React component is unmounted?

Sometimes, we want to check if the React component is unmounted.

In this article, we’ll look at how to check if the React component is unmounted.

How to check if the React component is unmounted?

To check if the React component is unmounted, we can use a ref to keep track of the mounted value.

For instance, we write

function MyComponent(props) {
  const isMounted = useRef(false);

  useEffect(() => {
    isMounted.current = true;
    return () => {
      isMounted.current = false;
    };
  }, []);

  return <>...</>;
}

export default MyComponent;

to define the isMounted ref with

const isMounted = useRef(false);

Then we add

isMounted.current = true;

in the useEffect to set isMounted.current to true when it’s mounted.

We call useEffect with an empty array so the callback only runs when it’s mounted.

And we return a function that’s called when it’s unmounted in the useEffect callback.

So we set isMounted.current to false there.

Conclusion

To check if the React component is unmounted, we can use a ref to keep track of the mounted value.

Categories
React Answers

How to maintain state after a page refresh in React?

Sometimes, we want to maintain state after a page refresh in React.

In this article, we’ll look at how to maintain state after a page refresh in React.

How to maintain state after a page refresh in React?

To maintain state after a page refresh in React, we can save the state in session storage.

For instance, we write

const Comp = () => {
  const [count, setCount] = useState(1);

  useEffect(() => {
    setCount(JSON.parse(window.sessionStorage.getItem("count")));
  }, []);

  useEffect(() => {
    window.sessionStorage.setItem("count", count);
  }, [count]);

  return (
    <div className="App">
      <h1>Count: {count}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
    </div>
  );
};

to save the latest value of count to session storage with sessionStorage.setItem.

We watch the value of count and call sessionStorage.setItem whenever it changes with the useEffect hook.

To get the item after it refreshes, we call sessionStorage.getItem in the first useEffect callback, which is called with an empty array so it’s run only when the component first mounts.

Conclusion

To maintain state after a page refresh in React, we can save the state in session storage.

Categories
React Answers

How to use anchors with React Router?

Sometimes, we want to use anchors with React Router.

In this article, we’ll look at how to use anchors with React Router.

How to use anchors with React Router?

To use anchors with React Router, we can use the react-router-hash-link package.

We install it by running

npm install --save react-router-hash-link

Then we use it by writing

import { HashLink as Link } from "react-router-hash-link";

<Link to="home-page#section-three">Section three</Link>;

to use the HashLink component with the to prop set to a URL with the anchor.

Conclusion

To use anchors with React Router, we can use the react-router-hash-link package.

Categories
React Answers

How to share states between components using the useState hook in React?

Sometimes, we want to share states between components using the useState hook in React.

In this article, we’ll look at how to share states between components using the useState hook in React.

How to share states between components using the useState hook in React?

To share states between components using the useState hook in React, we can lift the state up to the component that contains both components.

For instance, we write

const Ancestor = () => {
  const [count, setCount] = useState(1);

  return (
    <>
      <DescendantA count={count} onCountChange={setCount} />
      <DescendantB count={count} onCountChange={setCount} />
    </>
  );
};

to add the count state and set that as the value of the count prop of the DescendantA and DescendantB components.

Conclusion

To share states between components using the useState hook in React, we can lift the state up to the component that contains both components.

Categories
React Answers

How to get data from the Material UI TextField component?

Sometimes, we want to get data from the Material UI TextField component.

In this article, we’ll look at how to get data from the Material UI TextField component.

How to get data from the Material UI TextField component?

To get data from the Material UI TextField component, we can get the input value from the onChange callback.

For instance, we write

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

import TextField from "@material-ui/core/TextField";

const Input = () => {
  const [textInput, setTextInput] = useState("");

  const handleTextInputChange = (event) => {
    setTextInput(event.target.value);
  };

  return (
    <TextField
      label="Text Input"
      value={textInput}
      onChange={handleTextInputChange}
    />
  );
};

export default Input;

to add a TextField with the onChange prop set to the handleTextInputChange function.

In it, we call setTextInput with the event.target.value property which has the input value.

Then we get the input value from the textInput state, which we set as the value of the value prop.

Conclusion

To get data from the Material UI TextField component, we can get the input value from the onChange callback.