Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react memo

/* this is for es6 onward */
/* React.memo is the new PureComponent */

import React, { memo } from 'react'

const MyComponent = () => {
 return <>something</>
}

export default memo(MyComponent)
Comment

react memo

function MyComponent(props) {
  /* render using props */
}
function areEqual(prevProps, nextProps) {
  /*
  return true if passing nextProps to render would return
  the same result as passing prevProps to render,
  otherwise return false
  */
}
export default React.memo(MyComponent, areEqual);
Comment

React.memo example:

export function MyReactComponent({ myNumber }) {
  return <p>{myNumber * 5}</p>
}

export default React.memo(MyReactComponent, function areEqual(
  prevProps,
  nextProps
) {
  if (prevProps.myNumber !== nextProps.myNumber) {
    return false
  }
  return true
})
Comment

React Memo

//index.js:
import { useState } from "react";
import ReactDOM from "react-dom/client";
import Todos from "./Todos";

const App = () => {
  const [count, setCount] = useState(0);
  const [todos, setTodos] = useState(["todo 1", "todo 2"]);

  const increment = () => {
    setCount((c) => c + 1);
  };

  return (
    <>
      <Todos todos={todos} />
      <hr />
      <div>
        Count: {count}
        <button onClick={increment}>+</button>
      </div>
    </>
  );
};

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
Comment

PREVIOUS NEXT
Code Example
Javascript :: farewell discord.js 
Javascript :: js use restrict 
Javascript :: Find a character between space with Regex in js 
Javascript :: discord buttons 
Javascript :: js === 
Javascript :: js array map 
Javascript :: js array 0 to n 
Javascript :: angular how to check a radiobox 
Javascript :: js local storage 
Javascript :: jquery validation on click 
Javascript :: escaped json to json javascript 
Javascript :: what is redis used for 
Javascript :: javascript timing events 
Javascript :: Select radio button through JQuery 
Javascript :: mongodb find array which does not contain object 
Javascript :: regex check if number is greater than 
Javascript :: json comments 
Javascript :: remove specific item from array 
Javascript :: javascript child element selector 
Javascript :: send sms using twilio in node 
Javascript :: axios post nuxt 
Javascript :: hex string to decimal string javascript 
Javascript :: jquery ui timepicker 
Javascript :: how to check if array 
Javascript :: console vuex data 
Javascript :: reduce object to array javascript 
Javascript :: parse date without timezone javascript 
Javascript :: dinamically add checked to checkbox 
Javascript :: eslint disable next line multiple rules 
Javascript :: link tag react 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =