Class Component React Dample Code Example


Example 1: create react component class

class MyComponent extends React.Component{     constructor(props){         super(props);     };     render(){         return(             <div>               <h1>               	My First React Component!               </h1>             </div>         );     } };

Example 2: props in react app

/* PASSING THE PROPS to the 'Greeting' component */ const expression = 'Happy'; <Greeting statement='Hello' expression={expression}/> // statement and expression are the props (ie. arguments) we are passing to Greeting component  /* USING THE PROPS in the child component */ class Greeting extends Component {   render() {     return <h1>{this.props.statement} I am feeling {this.props.expression} today!</h1>;   } }  -------------------------------------------- function Welcome(props) {   return <h1>Hello, {props.name}</h1>; }  const element = <Welcome name="Sara" />; ReactDOM.render(   element,   document.getElementById('root') );

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?