Last active
November 18, 2021 01:03
-
-
Save kresnasatya/176bcbb706e6c5b7bfd3207ef64b8b64 to your computer and use it in GitHub Desktop.
Kenapa React.memo tidak bekerja di komponen Button?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { memo, useState } from "react"; | |
const App = () => { | |
const [greeting, setGreeting] = useState('Hello React!'); | |
const [count, setCount] = useState(0); | |
const handleIncrement = () => { | |
setCount(currentCount => currentCount + 1); | |
}; | |
const handleDecrement = () => { | |
setCount(currentCount => currentCount - 1); | |
}; | |
const handleChange = event => setGreeting(event.target.value); | |
return ( | |
<div> | |
<Headline headline={greeting} /> | |
<Input value={greeting} onChangeInput={handleChange}/> | |
<Count count={count}/> | |
<Button handleClick={handleIncrement}>Increment</Button> | |
<Button handleClick={handleDecrement}>Decrement</Button> | |
</div> | |
) | |
} | |
const Headline = ({ headline }) => <h1>{ headline }</h1>; | |
const Input = ({value, onChangeInput, children}) => ( | |
<label> | |
{children} | |
<input type="text" value={value} onChange={onChangeInput} /> | |
</label> | |
); | |
const Count = memo(({ count }) => { | |
console.log('Does it (re)render?'); | |
return <h1>Count: {count}</h1>; | |
}); | |
// Kenapa react memo tidak bekerja di komponen Button? | |
const Button = memo(({handleClick, children}) => { | |
console.log('Does it (re)render too?'); | |
return <button type="button" onClick={handleClick}>{children}</button>; | |
}); | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { memo, useState } from "react"; | |
import Headline from "./components/Headline"; | |
const App = () => { | |
const [greeting, setGreeting] = useState('Hello React!'); | |
const handleChange = event => setGreeting(event.target.value); | |
return ( | |
<div> | |
<Headline headline={greeting} /> | |
<Input value={greeting} onChangeInput={handleChange}/> | |
<Count /> | |
</div> | |
) | |
} | |
const Input = ({value, onChangeInput, children}) => ( | |
<label> | |
{children} | |
<input type="text" value={value} onChange={onChangeInput} /> | |
</label> | |
); | |
const Count = memo(() => { | |
console.log("does it re-render?"); | |
const [count, setCount] = useState(0); | |
const handleIncrement = () => { | |
setCount(currentCount => currentCount + 1); | |
}; | |
const handleDecrement = () => { | |
setCount(currentCount => currentCount - 1); | |
}; | |
return ( | |
<div> | |
<h1>Count: {count}</h1> | |
<button type="button" onClick={handleIncrement}>Increment</button> | |
<button type="button" onClick={handleDecrement}>Decrement</button> | |
</div> | |
); | |
}); | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment