Author -  Sai gowtham

Dynamically render components in React

In this tutorial, we are going to learn about how to dynamically add or remove components in react with the help of examples.

First, we need to create two new components so that we can use them for dynamic rendering.

userA.js
import React from "react";

function UserA() {
  return (
    <div>
      <h1>This is user A</h1>
    </div>
  );
}

export default UserA;
userB.js
import React from "react";

function UserA() {
  return (
    <div>
      <h1>This is user B</h1>
    </div>
  );
}

export default UserB;

Creating Dynamic component

Now, we need to create a dynamic component that helps us to render the other components dynamically based on the props.

Dynamic.js
import React from "react";
import UserA from "./userA";
import UserB from "./userB";

const components = {
  usera: UserA,  userb: UserB};

function DynamicComponent(props) {
  const SelectUser = components[props.user];  return <SelectUser />;
}

export default DynamicComponent;

In the above code, we first imported two components (UserA, UserB) then we added it to the components object.

Inside the DynamicComponent we created a SelectUser variable and returned it, so that if we pass usera as a prop UserA component is rendered or if we pass userb as a prop UserB component is rendered on the screen.

Using Dynamic component

Let’s use our dynamic component now by importing it.

App.js
import React, { useState } from "react";
import DynamicComponent from "./Dynamic";

function App() {
  const [user, changeUser] = useState("usera");
  return (
    <div>
      {/* initially UserA component is rendered */}
      <DynamicComponent user={user} />      <button onClick={() => changeUser("usera")}>Switch user-a</button>      <button onClick={() => changeUser("userb")}>Switch user-b</button>    </div>
  );
}

export default App;

Here we are switching between two components dynamically by clicking on the buttons.

react dynamic component example

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY