Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usecallback in react

//useCallback is hook that return memorized version of callback function
//only changes when one of dependency is changed
import {useState,useCallback} from 'react'
const [increment,setIncrement]=useState(0)
const [otherCounter,setOtherCounter]=useState(0) 
//useCallback(callback,dependencies)
const increment= useCallback(()=> {
  setCount(count+1)
},[count])

const incrementOtherCounter= useCallback(()=> {
setOtherCounter(otherCounter+1)
},[otherCounter])
Comment

react usecallback

const memoizedCallback = useCallback(
  () => {
    doSomething(a, b);
  },
  [a, b],
);
Comment

usecallback react

import React, { memo, useCallback, useState } from 'react'

const Logger = memo((props) => {
  props.log()
  return null
})

export default function App() {
  const [count, setCount] = useState(0)
  const count5 = Math.floor(count / 5)

  const memoizedFunction = useCallback(() => {
    console.log('useCallback')
  }, [count5])

  const normalFunction = () => {
    console.log('normal')
  }

  return (
    <>
      <button
        onClick={() => {
          setCount(count + 1)
        }}
      >
        Increment {count}
      </button>
      <Logger log={memoizedFunction} />
      <Logger log={normalFunction} />
    </>
  )
}
Comment

usecallback hook

//useCallback to remove too much re-render  
const checkFromLocalStorage = useCallback(() => {
    if (localStorage.getItem('connectedWallet')) {
      //check connectWallet with switch
      switch (localStorage.getItem('connectedWallet')) {
        case 'walletConnect':
          activate(WalletConnect);
        case 'metamask':
          activate(Injected);
        default:
      }
    }
  }, [active]);

  useEffect(() => {
    checkFromLocalStorage();
  }, [active]);
Comment

React useCallback Hook

//todos.js
import { memo } from "react";

const Todos = ({ todos, addTodo }) => {
  console.log("child render");
  return (
    <>
      <h2>My Todos</h2>
      {todos.map((todo, index) => {
        return <p key={index}>{todo}</p>;
      })}
      <button onClick={addTodo}>Add Todo</button>
    </>
  );
};

export default memo(Todos);
Comment

react import useCallBack

import { useCallback } from 'react'
Comment

usecallback in react

useCallback is a react hook which is used for the memorisation of the callback
function as we know in react every component re-rendered so its function also re 
created and so avoid the recreation of complex functions we used the concept of
useCallback which takes a function as a arguement and a dependency list for 
which condition the component are going to create itself;
Comment

PREVIOUS NEXT
Code Example
Javascript :: lodash merge 
Javascript :: scroll value bottom js 
Javascript :: jquery set multiple options selected 
Javascript :: js sleep function with argument 
Javascript :: flutter or react native 
Javascript :: toastandroid react native 
Javascript :: javascript update text in div 
Javascript :: execute command js 
Javascript :: canvas text gradient 
Javascript :: fetch from vscode 
Javascript :: javascript function from string 
Javascript :: get index in map javascript 
Javascript :: how to serve css files express 
Javascript :: show password fa-eye javascript 
Javascript :: express session mongoose 
Javascript :: discord js remove reaction from user 
Javascript :: async wait for axios reactjs 
Javascript :: check fpr multiple values in an array jquery 
Javascript :: js in_array 
Javascript :: js select keys from object 
Javascript :: react-loader-spinner 
Javascript :: how to check if something is array javascript 
Javascript :: foreach loop in nodejs 
Javascript :: pause javascript 
Javascript :: javascript mysql query 
Javascript :: try catch async await 
Javascript :: format json command line 
Javascript :: recieve voice channel audio discord.js 
Javascript :: bootstrap 4 modal popup remote url 
Javascript :: logical operators in js 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =