Below is a sample program where we can use state and props among class components and functional components.
I have created a new folder named Person.
Create a new file named Person.js in Person folder and below code in it. This is a functional component and we are using props in this. props.children displays the value that we pass between child component tags in the parent component.
import React from 'react';
const Person = (props) => {
return (
<div>
My name is {props.name} and I stay in {props.location}! {props.children}.
</div>
);
}
export default Person;
Paste the below code in App.js file.
import React from 'react';
import ReactDOM from 'react-dom';
import './App.css';
import Person from './Person/Person';
class App extends React.Component {
state = {
person:[
{name: "Paul", location: "New York"},
{name: "David", location: "Atlanta"},
{name: "Raj", location: "London"}
],
otherDetails: "I am working as Software Developer"
};
render(){
return (
<div>
<Person name={this.state.person[0].name} location={this.state.person[0].location} />
<Person name={this.state.person[1].name} location={this.state.person[1].location} >{this.state.otherDetails}</Person>
<Person name={this.state.person[2].name} location={this.state.person[2].location} />
</div>
);
}
}
export default App;




