Fade out a component in Material UI/React by setting a timeout in the parent's useEffect hook for when it unmounts

Incorporating a fade out transition into my child component, <Welcome />, using Material UI's Fade component is my current goal. This transition should be triggered by two specific conditions:

  1. The timeout set in the useEffect function expires.
  2. When the user clicks on a different element (in this case, it's Pano).

Originally, I had the component handling its own state logic. However, to address the second condition, I realized that the conditional logic needed to be moved to the parent component and passed down as props.

function App() {
 const [isVisible, setIsVisible] = useState(true);

 useEffect(() => {
    setTimeout(() => {
      setIsVisible(false);
    }, 8000);
  }, []);

return (
<div>
  <Fade in={isVisible} exit={!isVisible}>
    <Welcome setIsVisible={isVisible} isVisible={isVisible} />
  </Fade>
  <Pano ... />
</div>

The child component responsible for fading in/out looks like this:

export const Welcome = (props) => {
  const shown = props.isVisible

  return shown ? (
    <div className="wrapper">
      <img src={...} alt="welcome instructions" />
      <p>Click and drag to explore.</p>
      <p>Select icons for more information.</p>
    </div>
  ) : <div />;
};

Answer №1

After some consideration, I decided to switch over to Framer Motion for this task. It became apparent that all I really needed was the exit animation, as I already had a gentle blinking effect on the component when it initially loaded. By adjusting the fade speed on the first keyframe, I was able to achieve the desired result. Using Framer's <AnimatePresence> with a timeout function worked perfectly for this situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating a document using the nano module in CouchDB is not supported

I am currently utilizing the Node.js module known as nano Why is it that I am unable to update my document? I need to set crazy: true and then change it back to false. This is the code I have: var nano = require('nano')('http://localhost ...

How can I safeguard my HTML and CSS content from being altered using tools similar to Firebug?

Is there a method to deter the alteration of HTML and CSS components on a webpage using tools similar to Firebug? I have noticed that certain users are changing values in hidden fields and modifying content embedded within div or span tags for their perso ...

The MUI DataGrid Pagination has been replaced due to an error: validateDOMNesting(...): <td> should not be nested within a <div>

I'm struggling with replacing the pagination in my DataGrid component from Material-UI. Every time I try, I encounter this console error: Warning: validateDOMNesting(...): <td> cannot appear as a child of <div> I've double-checked my ...

Sort information based on chosen dropdown settings

I am relatively new to working with Reactjs. My current project involves displaying data on a homepage along with a dropdown containing various filtering options. I want the user to be able to select an option from the dropdown, which will then filter the ...

A guide on retrieving the upload status of a file using an AJAX post request

Is there a way to retrieve the status of uploaded files when the user cancels the process while uploading multiple files using an ajax call? This is how I am currently making the ajax request to upload files: var request = $.ajax({ url: 'file ...

Master the integration of Flask API with React-Redux by effectively implementing axios

Hello everyone! I am new to the world of React-Redux and I am currently working on a classification app using Flask API with ReactJS as the front-end. However, I am facing some issues when trying to call the API in Redux through the action file. The predi ...

Coordinated Universal Time on the Website

I am currently developing a website that will be exclusively accessible through the intranet, but it targets users across Australia. Recently, I have been instructed to explore the idea of incorporating UTC time on the site. I am contemplating how I can i ...

Increasing specificity to override styles in Material UI aesthetically

Is there a way to override a highly specific class rule? Take, for instance, the .MuiAccordionSummary-content.Mui-expanded class within the AccordionSummary const useStyles = makeStyles(() => ({ expanded: { marginBottom: 0, }, })); whe ...

What causes loss of focus in a React input element when it is nested inside a component?

When I have an input element connected to the React Context API, updating the value onChange works fine when the element is not nested within a component. However, if I render a different component just under the input that returns another input field conn ...

Include the selected class in the relative URL

I am attempting to apply a highlight class to my selected category: My code looks like this : <div id="category"> <ul> <a href="category.php?c=electronic"> <li>Electronic</li> </a> ...

Nashorn poses a security threat due to its ClassFilter vulnerability

Encountering some troubles with Nashorn and came across a concerning security vulnerability highlighted here: It appears that someone can easily execute code using this command: this.engine.factory.scriptEngine.eval('java.lang.Runtime.getRuntime().ex ...

What is the best way to show JSON array data in jQuery without using double quotes?

I have encountered an issue where I need to remove the double quotes from my json_encode output. To achieve this, I have used the following code: $json_string = json_encode($data); $json_string = str_replace('"','',$json_string); echo ...

Finding the variance in the given situation is as simple as following these steps

To find the variance between the "Total Marks" in a question and the input texts for each answer, we need to consider specific scenarios. Firstly, if a question has only one answer, the text input should be displayed as readonly with the same value as the ...

Obtain the refined information from a UiGrid table

Is there a way to loop through the filtered rows in an uigrid? I am aware that we can get the filtered rows using the following code: var filteredData = $scope.gridApi.core.getVisibleRows($scope.gridApi.grid); However, I need to iterate through it and cr ...

Setting a backgroundImage URL using props in React is a useful feature that allows for

Trying to create a React component that changes its background image based on the props received. Any suggestions on how to achieve this functionality? The props object includes a property called synmain, which is a string linking to an image "../image.p ...

What is the best way to integrate a condition for handling invalid or empty input in a Contact Form when using FormSpree?

Utilizing Formspree to create a basic Contact form on my NextJS site. Formspree offers this React code sample: // Don't forget to execute npm install @formspree/react // For additional assistance, go to https://formspr.ee/react-help import React from ...

Handling OnClick events in D3 with Websocket Integration

My goal is to implement a Websocket in JavaScript that transmits a variable obtained when clicking on a node in a D3 chart. While I have made progress using static data, I'm struggling with initiating the code upon node click to retrieve the "user inf ...

Using AngularJS to populate dropdown options with data from JSON objects

I have a JSON file that looks like this: var myJson = { "Number of Devices":2, "Block Devices":{ "bdev0":{ "Backend_Device_Path":"/dev/ram1", "Capacity":"16777216", "Bytes_Written":9848, "timestamp":"4365093 ...

The functionality of both P and H1 tags seems to be malfunctioning

Can someone help me with changing the font family and size of my paragraphs and headings (p and h1)? Any assistance would be greatly appreciated. Feel free to review my code for any other errors as it has been a while since I last coded. html, body { ma ...

The act of selecting a parent element appears to trigger the selection of its child elements as well

I am attempting to create an accordion using Vanilla JavaScript, but I have encountered an issue. When there is a child div element inside the header of the accordion, it does not seem to work and I'm unsure why. However, if there is no child div elem ...