props and state in react js code example

Example 1: state and props

What is the Props
Props is short for properties and they are used to pass data between React components. React’s data flow between components is uni-directional
wt is State
which allows components to create and manage their own data

Example 2: state with react functions

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      

You clicked {this.state.count} times

); } }

Example 3: how to define state in react function

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count" 
  const [count, setCount] = useState(0);
  return (
    

You clicked {count} times

); }

Example 4: props and state react

function tick() {
  const element = (
    

Hello, world!

It is {new Date().toLocaleTimeString()}.

); ReactDOM.render( element, document.getElementById('root') );} setInterval(tick, 1000);

Tags:

Misc Example