PeakNotes - Your Ultimate Resource for Coding Tips and Tutorials

React js


                                   react


useState is type of reactjs hooks

The React useState Hook allows us to track state in a function component.

State generally refers to data or properties that need to be tracking in an application.


import React from "react";
import { useState } from "react";

function Hh (){

   let [counter , setCounter]= useState(10)

    // let counter = 5

    const addValue = () =>{
        // counter = counter + 1;
        setCounter(counter + 1);
    }

    const removeValue  =()=>{
        counter=counter - 1;
        setCounter(counter);
    }
        return(
            <>
                <h1>Chai or react</h1>
                <h2>counter value : {counter}</h2>

                <button onClick={addValue}>Add value</button>
                <br/>
                <button onClick={removeValue}>Remove value</button>
            </>
        )
}

export default Hh;

usememo

import React,{useMemo, useState} from "react";

function Hp(){

    const [count,setCount]=useState(0)
    const [item,setItem]=useState(10)

    let multiCountMemo=useMemo(function multiCount(){
        return count*5;
    },[count])
   
    return(
        <>
            <h1>harshvardhan </h1>
            <h2>count : {count}</h2>
            <h2>Item : {item}</h2>
            <br/>
            {multiCountMemo}
            <br/>
            <button onClick={()=>setCount(count+1)}>Update count</button>
            <button onClick={()=>setItem(count*10)}>Update Item</button>
        </>
    );
}


export default Hp;

useeffect


import React, { useState, useEffect } from "react";

const UseEffect = () => {
  const [count, setCount] = useState(0);

  const countUpdate = (val) => {
    if (val === "inc") return setCount(count + 1);
    if (val === "dec") return setCount(count - 1);
  };

  useEffect(() => {
    document.title = count;
  }, [count]);

  return (
    <>
      <Wrapper>
        <div className="container">
          <button onClick={() => countUpdate("inc")}>
          </button>
          <h1>{count}</h1>
          <button onClick={() => countUpdate("dec")}>
            <FaMinus className="icon minus_icon" />
          </button>
        </div>
      </Wrapper>
    </>
  );
};

export default UseEffect;

thanks 

Post a Comment

0 Comments