props or properties in React is a way in which we can pass data between components in a React page. Usually props are passed from the parent component to the child/nested components. props are immutable they are passed from the parent/container component to the child components.
In this example we shall see on how to pass props in a parent component and pass it to the child/nested component.
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class ChildComponent extends Component {
render() {
return (
<div>
Prop value from parent component: <b> {this.props.parentProp} </b>
</div>
);
}
}
class HelloProps extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<div>
<ChildComponent parentProp = "Hello Prop" />
</div>
</div>
);
}
}
ReactDOM.render(<HelloProps />, document.getElementById('app'));
In this example we shall see on how to pass props in a parent component and pass it to the child/nested component.
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class ChildComponent extends Component {
render() {
return (
<div>
Prop value from parent component: <b> {this.props.parentProp} </b>
</div>
);
}
}
class HelloProps extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<div>
<ChildComponent parentProp = "Hello Prop" />
</div>
</div>
);
}
}
ReactDOM.render(<HelloProps />, document.getElementById('app'));
No comments:
Post a Comment