Categories
React Answers

How to reset a React element?

Sometimes, we want to reset a React element.

In this article, we’ll look at how to reset a React element.

How to reset a React element?

To reset a React element, we can change the value of the key prop of the element.

For instance, we write:

import React, { useState } from "react";

export default function App() {
  const [key, setKey] = useState();

  return (
    <div>
      <button onClick={() => setKey(Math.random())}>reload</button>
      <div key={key}>{Math.random()}</div>
    </div>
  );
}

We define the key state with the useState hook.

Then we add a button with the onClick prop set to a function that calls setKey with a random number.

Next, we assign the key state as the value of the key prop.

As a result, we should see the random number in the div change when we click on the button.

Conclusion

To reset a React element, we can change the value of the key prop of the element.

Categories
React Answers

How to fix the can not submit form with react-bootstrap issue?

Sometimes, we want to fix the can not submit form with react-bootstrap issue.

In this article, we’ll look at how to fix the can not submit form with react-bootstrap issue.

How to fix the can not submit form with react-bootstrap issue?

To fix the can not submit form with react-bootstrap issue, we should wrap the form element around the Bootstrap form components.

Then we set the onSubmit prop of the form element to a function that calls event.preventDefault to prevent the default server-side submission behavior.

For instance, we write:

import React from "react";
import { FormGroup, Button, FormControl } from "react-bootstrap";

export default function App() {
  const gotEmail = (event) => {
    event.preventDefault();
    console.log("got email");
  };

  return (
    <div>
      <form onSubmit={gotEmail}>
        <FormGroup role="form">
          <FormControl type="text" className="form-control" />
          <Button
            className="btn btn-primary btn-large centerButton"
            type="submit"
          >
            Send
          </Button>
        </FormGroup>
      </form>
    </div>
  );
}

We wrap the form element around the FormGroup element to create the form.

Then we set the onSubmit prop to the gotEmail function.

In gotEmail, we call event.preventDefault to prevent the default server-side form submission behavior.

As a result, when we click Send, we see 'got email' logged instead of submitting the form on server-side and reloading the page.

Conclusion

To fix the can not submit form with react-bootstrap issue, we should wrap the form element around the Bootstrap form components.

Then we set the onSubmit prop of the form element to a function that calls event.preventDefault to prevent the default server-side submission behavior.

Categories
React Answers

How to insert into React’s state array with useState?

Sometimes, we want to insert into React’s state array with useState.

In this article, we’ll look at how to insert into React’s state array with useState.

How to insert into React’s state array with useState?

To insert into React’s state array with useState, we call the state setter function with a callback that returns the new state array with the value we want inserted.

For instance, we write:

import React, { useState } from "react";

export default function App() {
  const [myArray, setMyArray] = useState([]);

  const insert = () => {
    setMyArray((myArray) => [...myArray, Math.random()]);
  };

  return (
    <div>
      <button onClick={insert}>insert</button>
      {myArray.map((a) => (
        <p key={a}>{a}</p>
      ))}
    </div>
  );
}

to define the myArray state array with useState set to an empty array as its initial value.

Then we define the insert function that calls setMyArray with a callback that returns the new array with a random number attached at the end of it.

Next, we add a button that has the onClick prop set to insert.

And finally we render the myArray values with myArray.map.

As a result, when we click insert, we should see new numbers displayed.

Conclusion

To insert into React’s state array with useState, we call the state setter function with a callback that returns the new state array with the value we want inserted.

Categories
React Answers

How to clear browser cache in React?

Sometimes, we want to clear browser cache in React.

In this article, we’ll look at how to clear browser cache in React.

How to clear browser cache in React?

To clear browser cache in React, we can get the cache data from the window.caches property.

Then we call then with a callback that calls delete to clear the cache. For instance, we weite:

import React, { useEffect } from "react";

export default function App() {
  useEffect(() => {
    if ("caches" in window) {
      caches.keys().then((names) => {
        names.forEach((name) => {
          caches.delete(name);
        });
      });
    }
  }, []);

  return <div>hello world</div>;
}

We call caches.keys to get the cache object.

Then we call then with a callback that takes the names array with the which is set to the cache keys array.

Next, we call names.forEach with a callback that calls caches.delete with name to delete the cache entry.

Conclusion

To clear browser cache in React, we can get the cache data from the window.caches property.

Then we call then with a callback that calls delete to clear the cache.

Categories
React Answers

How to pass text between React component tags?

Sometimes, we want to pass text between React component tags.

In this article, we’ll look at how to pass text between React component tags.

How to pass text between React component tags?

To pass text between React component tags, we can add the children prop as a prop of the component we want to put text in between the tags of.

For instance, we write:

import React from "react";

const Foo = ({ children }) => <h1>{children}</h1>;

export default function App() {
  return <Foo>hello world</Foo>;
}

We create the Foo component that takes the children prop.

And we render children in between the h1 tags.

Next, we put ‘hello world’ in between the opening and closing Foo tags in App.

As a result, we should ‘hello world’ rendered between the h1 tags and we see it rendered in bold.

Conclusion

To pass text between React component tags, we can add the children prop as a prop of the component we want to put text in between the tags of.