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

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 :: Detect Pangram 
Javascript :: post method in javascript 
Javascript :: angular 7 selected 
Javascript :: chrome storage local update 
Javascript :: payfast javascript 
Javascript :: string sort javascript 
Javascript :: pagination react 
Javascript :: detect scroll height 
Javascript :: The reduce() method executes a reducer function on each element of the array and returns a single output value. 
Javascript :: js dictionary contains key 
Javascript :: react select dropdown 
Javascript :: Uncaught (in promise): NotReadableError: Could not start video source 
Javascript :: redirect all routes to main component vue 
Javascript :: global catch in javascript 
Javascript :: form in react 
Javascript :: array reduce 
Javascript :: javascript breakpoint 
Javascript :: how to fetch data from another website in javascript 
Javascript :: get last element in array javascript 
Javascript :: js iterating set 
Javascript :: react-router in saga 
Javascript :: javascript catch all click events 
Javascript :: mobile nav react npm 
Javascript :: mongodb where field is not equal 
Javascript :: angularjs ng-options name value 
Javascript :: js regex return null 
Javascript :: Angle Between Hands of a Clock 
Javascript :: prototype javascript 
Javascript :: http request body angular 
Javascript :: use the AJAX XMLHttpRequest object in Javascript to send json data to the server 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =