Ads Top

How to create dynamic component in the reactjs

How to use the dynamic component in react js?

Dynamic component or dynamically element we can create in react first need you will start new project Or if you have already project then go to in your “project > src folder”.

Then create a folder as name components also, you can create a file as navlist.js directly in the src folder. You can create filename as you wish. As below code with screenshot:

import React from 'react'; 
const Navlinks = (props) => {
        <li><a href={props.linkss}>{props.linkName}</a></li>
export default Navlinks;

Now you will create new file header.js Or as your requirement. Now import new component in this file and create new component Header and then you will write your JSON object in the state section. Create a function readerList Or as you want.

How to render any dynamic code in the react?
After creating component then need to render code most important thing that must use curly braces {} and then you will use your function with this keyword because you are accessing with the self code as:  {this.readerList()}

import React,{Component} from 'react';
import Navlinks from './navlist';
export default class Header extends Component{
    state = {
        navlistss: [
            {name:'Home', href:'index.html'},
            {name:'About', href:'about.html'},
            {name:'Services', href:'services.html'},
            {name:'Contact', href:'contact.html'}

    readerList = () => {
        return => <Navlinks linkName={} linkss={item.href} />);


Then go to your app.js file and call to Header component as given code and screenshot below:

import React from 'react';
import './App.css';

import Header from './components/header/header';

function App() {
  return (
      <div className="container"><Header /></div>
export default App;

After then you will run your npm server and go to the browser, you will get your dynamic navigation as you defined in the state as JSON file.

Also, we can create dynamic rendering with multiple objects using with one method as given below code:

import React from 'react'

export default (props=> <div className={props.cls}><img src={props.mysrc} alt={props.alt} /></div>

export default () => {
const textDetails = {

const rendertextDetails = (items=> {
        return => <BannerImages cls={item.cls} mysrc={images(item.mysrc)} alt={item.alt} />)

         { rendertextDetails(textDetails.animals)}
         { rendertextDetails(textDetails.dogs)}

No comments:

Copyright Reserved to Anything Learn. Powered by Blogger.