functional component react jS code example

Example 1: create react component class

class MyComponent extends React.Component{
    constructor(props){
        super(props);
    };
    render(){
        return(
            

My First React Component!

); } };

Example 2: functional component react

function Welcome(props) {
  return 

Hello, {props.name}

; } function App() { return (
); } ReactDOM.render( , document.getElementById('root') );

Example 3: create functional component react

import React from 'react'; const App = () => {  const greeting = 'Hello Function Component!';   return ;}; const Headline = ({ value }) =>  

{value}

; export default App;

Example 4: react class component

class Welcome extends React.Component {
  render() {
    return 

Bonjour, {this.props.name}

; } }

Example 5: react function component

function Welcome(props) {
  return 

Hello, {props.name}

; }

Example 6: class to functional component react

// convert a class component to a functional component
class MyComponent extends React.Component {
	state: { 
      name: 'Bob' 
    }
	render() {
      return (
        

Hello, my name is {this.state.name}

); } } const MyComponent = () => { const [name, setName] = React.useState('Bob'); return (

Hello, my name is {name}

); }

Tags:

Misc Example