Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmount, or hide in React?

What's more efficient? Should I mount and unmount components, or show and hide them with css?

The practical case where this has come up for me is with Modals. I can either keep the Modal's 'active' state in its parent component, and pass it along as a prop to the Modal which then maybe applies className 'active'. Or, I could have a modal 'navigator' that determines which modal to display, and its reference gets passed around to other components.

like image 228
alexbake Avatar asked Jul 17 '16 04:07

alexbake


People also ask

What is unmount in React?

Unmounting. The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it. React has only one built-in method that gets called when a component is unmounted: componentWillUnmount()

What triggers an unmount in React?

React is intended to have an unmount happen from a parent to child relationship. Now if you want a child to unmount itself, you can simulate this with a state change in the parent that is triggered by the child.

Does React router unmount component?

Don't Forget that Every Route Change Mounts and Unmounts a Component. Whenever you're using routing in a React application, you declare routes inside the Switch component. This means that only one component with the matching route is displayed at a time.

How do you hide an element in React?

To show and hide components and elements in React you will need to either use conditional rendering, css styles or animation libraries. For the most part conditional rendering will be done by checking if a value passes a condition, just like in an if statement but for react components and JSX.


1 Answers

It's a matter of preference, but I always return null if I don't need an item to be visible.

The main reason is that if you're doing server-side rendering, and you have a few hundred lines of HTML that is 'hidden' but still gets rendered, then you're sending a lot of unnecessary data over the wire.

A modal 'navigator' (or 'conductor' below) would sit as the last child at the top of your DOM tree and return the appropriate modal or null:

import React from 'react';

import ExportDataModal from './ExportDataModal.jsx';
import SignInModal from './SignInModal.jsx';
import FeedbackModal from './FeedbackModal.jsx';
import BoxDetailsModal from './BoxDetailsModal.jsx';

const ModalConductor = props => {
  switch (props.currentModal) {
    case 'EXPORT_DATA':
      return <ExportDataModal {...props}/>;

    case 'SOCIAL_SIGN_IN':
      return <SignInModal {...props}/>;

    case 'FEEDBACK':
      return <FeedbackModal {...props}/>;

    case 'EDIT_BOX':
      return <BoxDetailsModal {...props}/>;

    default:
      return null;
  }
};

export default ModalConductor;

Here's an article that goes into a bit of detail about Modals in React.

like image 164
David Gilbertson Avatar answered Nov 15 '22 00:11

David Gilbertson