Articles → REACT.JS → Props In React.Js
Props In React.Js
Purpose
Example
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
class ChildComponent extends React.Component {
render() {
return <h2>This is {this.props.property} attribute of the child component</h2>;
}
}
class ParentComponent extends React.Component {
render() {
return (
<div><ChildComponent property="test"/></div>
);
}
}
ReactDOM.render(<ParentComponent />, document.getElementById('root'));
Output
Click to Enlarge
Difference Between State And Prop
- State is private to the component whereas props are used to pass data from one component to the other.
- Props are read-only.