Create Component with JSX Pass data with Props - IndianTechnoEra
Latest update Android YouTube

Create Component with JSX Pass data with Props

 

Create Component with JSX Pass data with Props 

Aim: 

Build simple React components that implement a render () methods, which takes input data and returns data to display. Use an XML-like syntax called JSX. Input data, passed into the component can be accessed by render () via this.props.


Component 1 :

Welcome.js

import React from 'react'

class Welcome extends React.Component {

  constructor (props) {

    super()

    this.state = { message: 'You are Welcome: ' }

  }

  render () {

    return (

      <>

        <>

          <h1>{this.state.message} {this.props.name}</h1>

          <h2>New Name: {this.props.newname}</h2>

          <h2>Your city is : {this.props.city}</h2>

        </>

      </>

    )

  }

}

 

export default Welcome;


Component 2 :

index.js

import ReactDOM from 'react-dom/client'

import React from 'react'

import Welcome from './Welcome'

const root = ReactDOM.createRoot(document.getElementById('root'))

root.render(

  <>

    <React.StrictMode>

      <Welcome name='Shahnawaz' newname='Sam' city='Siwan'/>

    </React.StrictMode>

  </>

)



Output :


Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.