The MUI Fade component specializes in showcasing, concealing, and transitioning components without any fading effects

I have recently made a change in my code where I replaced conditional rendering of child components with wrapping them in <Fade />. However, the Fade effect does not seem to work as both components always display. Below is the updated code snippet:

// within the main component
{/*{adminView === bodyViews.addNewCoupon &&*/
<Fade
  in={Boolean(adminView === bodyViews.addNewCoupon)}
  timeout={2000}
>
  <AddCouponForm />
</Fade>
{/*}*/}
  
{/*{adminView === bodyViews.viewCurrentCoupons &&*/
<Fade
  in={Boolean(adminView === bodyViews.viewCurrentCoupons)}
  timeout={2000}
>
  <ViewCoupons />
</Fade>
{/*}*/}

Referring to the API documentation available here, setting in to true should trigger the fading effect for the component. While this worked in the previous version with conditional rendering, it seems that using it directly within the in attribute does not produce the expected outcome. What could be causing this issue?

Update:

I noticed that when I comment out the custom components and replace them with something simple like <p>Section 1/2</p>, the fade effect works correctly. There appears to be something specific to the custom components that prevents the Fade effect from working properly.

Answer №1

The problem was identified specifically with Custom components: they do not function properly as direct descendants of <Fade />. The issue has been resolved by enclosing the custom component children in a div.

  <Fade
    in={ Boolean(adminView === bodyViews.addNewCoupon) }
    timeout={ 4000 }
  >
    <div>
      <AddCouponForm />
    </div>
  </Fade>     

  <Fade
    in={ Boolean(adminView === bodyViews.viewCurrentCoupons) }
    timeout={ 4000 }
  >
    <div>
      <p>Section 2</p>
      <ViewCoupons />
    </div>
  </Fade>

Additionally, it appears that Fade encounters issues when there are multiple children. Thus, all children should be contained within a single div element.

Answer №2

Transition allows the child component to be updated using the styles prop. If you are working with a custom component, it is important to pass down the props from Transition by forwarding the ref to the child:

const CustomComponent = React.forwardRef((props, ref) => {
  return (
    <div {...props} ref={ref}>
      custom component
    </div>
  );
});
<Transition in={checked}>
  <CustomComponent />
</Transition>

Interactive Example

https://codesandbox.io/s/57078732-custom-transition-component-demo-ji8ws?file=/demo.tsx

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

Filtering tables with checkboxes using Next.js and TypeScript

I've recently delved into Typescript and encountered a roadblock. While I successfully tackled the issue in JavaScript, transitioning to Typescript has left me feeling lost. My dilemma revolves around fetching data from an API and populating a table u ...

Managing the URLs of single page applications

Typically in a Single Page App (SPA), there is usually one main page that contains a side navigation menu with various anchor tags. These anchor tag URLs are managed by the angular/react/sammy js router, and the content of the main section is updated based ...

Implementing a Scalable Application with React Redux, Thunk, and Saga

What sets Redux-Saga apart from Redux-Thunk? Can you explain the primary use of redux saga? What exactly is the objective of redux thunk? ...

Is it crucial to commit to memory a Tiny/Affordable Component? - ReactJS

Imagine having a child component that simply returns a div element Child Component : function Child() { return <div className={styles.alertBox}>No Data Found</div>; } Every time the parent state changes, this child component will re-render. ...

Create a left-aligned div that spans the entire width of the screen, adjusting its width based on the screen size and positioning it slightly

I have a parent container with two child elements inside. I want the first child to align to the left side and the second child to align to the right side, but not starting from the exact center point. They should be positioned slightly off-center by -100p ...

Adjusting runtime environment variables for a React application

I have a React Application that needs to be deployed to Cloud Foundry and I am looking for a way to segregate the development, staging, and production environments. The challenge is that React only provides .env.development for running the application usin ...

Enhance Material Ui by including the essential attribute to a TextField while in "select" mode

I'm attempting to make the "required" attribute work for a TextField in select mode. I attempted to add the required prop as shown in this code snippet, but it doesn't prevent the form submission if nothing is selected. However, it does add an as ...

Content within innerHTML not appearing on the screen

I'm facing an issue with displaying an error message when the user enters incorrect username or password. The message should pop up below the sign-in box, but it's not showing up for some reason. Can anyone help me figure out why? Here is my cod ...

Creating a Robust Next.js Build with Tailor-Made Server (Nest.js)

I'm in need of assistance with compiling/building my project using Next.js while utilizing a custom server. Currently, I have integrated Nest.js (a TypeScript Node.js Framework) as the backend and nested Next.js within it. The integration seems to be ...

Utilizing shared references across react-imask, react-bootstrap, and react-hook-form for a seamless integration

I have integrated react-bootstrap and react-imask using their example with IMaskMixin as shown below: import { IMaskMixin } from 'react-imask'; import { FormControl } from 'react-bootstrap'; const MaskedFormControl = IMaskMixin(({input ...

Leveraging Trustpilot TrustBoxes within a Next.js environment

I'm currently implementing a Trustpilot TrustBox into a Next.js application. Inside my componentDidMount, I have the following code: var trustbox = document.getElementById('trustbox'); window.Trustpilot.loadFromElement(trustbox); In the ...

Issue encountered when attempting to send FormData from Next.js API to Express.js backend resulting in Error 405: Bad Request

SOLVED / SEE MY OWN ANSWER FOR DETAILS Encountering a perplexing error that has left me stuck. I am currently facing an issue where I am trying to send an audio recording captured by the user in my Next.js Frontend to my Next.js API, which appears to be f ...

Attempting to create a TypeScript + React component that can accept multiple types of props, but running into the issue where only the common prop is accessible

I am looking to create a component named Foo that can accept two different sets of props: Foo({a: 'a'}) Foo({a: 'a', b: 'b', c:'c'}) The prop {a: 'a'} is mandatory. These scenarios should be considered i ...

React Native ScrollView ref issue resolved successfully

I'm trying to automatically scroll to the bottom of a flatlist, so here's what I have: const scrollViewRef = useRef(); //my scroll view <ScrollView ref={scrollViewRef} onContentSizeChange={() => { scrollViewRef.current.scr ...

Tailored design - Personalize interlocking elements

I am currently working on a custom theme and I am trying to adjust the font size of Menu items. In order to achieve this, I have identified the following elements in the tree: ul (MuiMenu-list) MuiListItem-root MuiListItemText-root If I want to modify th ...

Resetting forms in React upon submission

After successfully submitting a form using Axios, I'm facing an issue where the data in each text field remains on the page instead of getting cleared. I attempted to incorporate a resetForm function to reset the form back to its original blank state, ...

Issue with SMS_MFA not being enabled due to missing delivery configuration in React and AWS Amplify

My React web application, powered by aws-amplify, interacts with AWS Cognito User Pool for user authentication. Users can choose to activate SMS MFA from the app settings. While attempting to enable SMS MFA using the aws amplify npm package, I encountere ...

Error: 'Target is not found' during React Joyride setup

I am attempting to utilize React Joyride on a webpage that includes a modal. The modal is supposed to appear during step 3, with step 4 displaying inside the modal. However, I am encountering an issue where I receive a warning message stating "Target not m ...

When working in React, I encountered a problem with the for of loop, as it returned an error stating "x is undefined." Although I could easily switch to using a simple for loop, I find the for of loop to

I'm encountering an issue when using a for of loop in React, as it gives me an error stating that "x is undefined". import { useEffect } from "react"; export default function HomeContent() { useEffect(() => { let content = document ...

React Native: Avoiding Infinite Loops in useEffect

I am facing an issue where my app goes into an infinite loop because pointsData is inside the useEffect function. How can I resolve this situation? function useGetPoints() { const [pointsData, setPointsData] = useState<PointTbleType[]>([]); ...