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

Ways to incorporate ejs partials using JavaScript

I am currently developing code to determine if a user is logged in. Depending on the user's login status, the content of the "my user" section should vary. When a logged-in user navigates to the "my user" page, an if statement is executed to confirm ...

Looking for a solution to eliminate Parsing Error: Unexpected Token when developing with React? I've already implemented ESLint and Prettier

Currently, I am working on building a form in React with MUI. Below is the code snippet that I have been working with: const textFields = [ { label: 'Name', placeholder: 'Enter a company name', }, { label: 'Addres ...

Switch between the table data elements in an .hta document

The response provided by Dr.Molle in a previous post here was accurate, but it only functioned with <div>. I am required to utilize <table>. Though I found a script that works flawlessly outside of my VBScript, it does not work when embedded in ...

Using masonry-layout with Next Js leads to a ReferenceError stating that window is not defined

Implementing the masonry-layout library by David Desandro in my Next app has been a smooth process. You can find the link here. When I apply it, the masonry layout functions perfectly as intended. Here's how I'm incorporating it successfully: imp ...

Ensuring Radiobuttons are Selected on a Page After Clicking the Submit Button

Seeking assistance with this issue: I am working on a page that includes radio buttons: <label>Delivery type:</label> <input type="radio" name="delivery" value="7" class="delivery-item" id="del-type-7" onclick=""><label class="label" ...

Is there a way to create an X shape by rotating two divs when I click on the container div?

I currently have this setup: code Below is the HTML code: <div id="box"> <div id="line1" class="line"></div> <div id="line2" class="line"></div> <div id="line3" class="line"></div> </div> My go ...

for each and every object, execute the asynchronous function

I have a scenario where I need to loop through objects, call an async method ("search") with a callback, and then write the results (resultSet) to a JSON file once all objects are processed. The issue I'm facing is that the writeFile function is execu ...

Moving Text Over a Static Background Image in HTML and CSS

Currently working on developing a website that involves coding in HTML and CSS. However, I've encountered an issue while programming. Whenever I attempt to adjust the positioning of my text, the background image shifts along with it, making it impossi ...

What is the best way to utilize a single npm module in multiple TypeScript files?

Question: I keep encountering the error message "error TS2451: Cannot redeclare block-scoped variable 'os'" when I try to import the same npm module in multiple TypeScript files and run the TypeScript compiler tsc. Here is an overview of my proj ...

Tips for building a custom error handling function specifically designed for parsing and handling errors in

Is there a way to reduce redundancy in my code, such as: let name = ""; try { name = data[" "][0][" "][0][" "][0][" "][1][" "][0][" "][1]["C"]; } catch (error) { if (error instanceof TypeError) { console.log("name TypeError"); } } I have consid ...

Encountered an issue while trying to install npm package express-generator globally

Encountering an error when trying to run npm install express? Looking for the correct solution? Read on. -bash-3.2$ npm install express-generator -g npm WARN engine <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a2bfb7b5a2 ...

Controlling the access to simpleJWT tokens in Django-React authentication to restrict user permissions

My backend has two React frontends: one for general users and the other specifically for admin users with the is_staff attribute. I have customized TokenObtainPairSerializer to add extra fields like 'is_staff'. Now, I'm considering how to r ...

In ReactJs, encountering an error when trying to access the 'length' property of null

I have been struggling with this error for over an hour now and I can't seem to find a solution. Is there something crucial that I am overlooking? I thought using optional chaining on the array mapping would fix this issue, but it's still not wor ...

connect the validation of forms to different components

I am currently working on components to facilitate the user addition process. Below is an example of my form component: createForm(): void { this.courseAddForm = this.formBuilder.group({ name: ['', [ Validators.required, ...

Error handling in Mongoose callback functions

Currently, I am delving into nodejs, express and mongoose. A question has arisen in my mind regarding the findOne function used to fetch a document from the database. Typically, it is utilized like this: Product.findOne({_id: req.params.id},function(erro ...

Using Jmeter's JSON Extractor for parsing response and extracting token value

Currently facing an issue with extracting the "webToken" response. I have attempted using both $..webToken and $.webToken as JSON path expressions, but no luck so far. Any suggestions on how to correctly extract this information? This is for use in JMete ...

After receiving a data token from the server in one controller, how can I efficiently utilize that token in a different AngularJS controller?

In my adminSearchCtrl controller, I am receiving data from the server in the form of a token and want to pass that token to another controller named "adminViewCtrl". How can I achieve this? adminSearchCtrl.js $scope.getUserDetails = function(selectedUser ...

I keep running into an issue whenever I attempt to import material ui icons and core - a frustrating error message pops up stating that the module cannot be found

[I keep encountering this error message when attempting to utilize @material-ui/core and icons] `import React from "react"; import "./Sidebar.CSS"; import SearchIcon from "@material-ui/icons/Search"; const Sidebar = () => { return ( <> ...

Having trouble getting a Custom React Component from an external library to work with MUI styling

I am currently working with a React component that comes from a module located in the node_modules folder: type Props = { someProps: any }; export function ComponentA(props: Props) { const [value, setValue] = React.useState(""); return ( <Te ...

Arrange a pair of div containers side by side on the webpage without the need to alter the existing

Is there a way to align these two div side by side? <div class="main_one"> <div class="number_one">Title A</div> </div> <div class="main_two"> <div class="number_two">Title B</div> </div> <div class=" ...