-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with memo #416
Comments
Hi @ramnivas, maybe you know why this happens. Thanks in advice! |
By default React performs shallow compare of the props (and for |
@ramnivas something strange is happening to me, the same js code works without use comparison. it's something special from slinky? https://stackblitz.com/edit/react-sl3mi8 import React from "react";
import "./style.css";
//import { useState } from 'react';
const { useState } = React;
export const Small = React.memo(({value}) => {
console.log("Refresh Component")
return (
<small>{ value }</small>
)
})
export const MemorizeApp = () => {
const [count, setCount] = useState(0)
const [show, setShow] = useState(true)
return (
<div>
<h1><Small value={ count } /></h1>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
<button onClick={() => setShow(!show)}>
Show {JSON.stringify(show)}
</button>
</div>
)
}
export default function App() {
return (<MemorizeApp />
);
} |
How would it be solved in this use case? Here my real use case, see Father and open console https://mvillafuertem.github.io/zio-react/hooks/ @react object CallbackHookApp {
type Props = Unit
val component: FunctionalComponent[Props] = FunctionalComponent[Props] { _ =>
val (counter, setCounter) = useState[Int](0)
val incrementCallback: () => Unit =
useCallback(() => setCounter(counter => counter + 1), Seq(setCounter))
Fragment(
h1("CallbackHookApp"),
h3(counter),
ShowIncrement(incrementCallback)
)
}
}
@react object ShowIncrement {
case class Props(increment: () => Unit)
val component: FunctionalComponent[Props] = React.memo(
FunctionalComponent[Props] {
case Props(increment) =>
println("Refresh Component, only show when the props changes")
button(className := "btn btn-success", onClick := increment)("+1")
},
(oldProps: Props, newProps: Props) => oldProps.increment eq newProps.increment
)
}
|
this is such a great reference example on SyntheticMouseEvent as well as @mvillafuertem call me lazy, but I usually solve issues like this with a surrogate So essentially add a prop called There might be a way to compare functions in ScalaJS, but... laziness :) |
Hi all, I am experiencing component refresh even though I am using React.memo. Am I wrong about something?
Here my use case, see Memorize and open console https://mvillafuertem.github.io/zio-react/hooks/
The text was updated successfully, but these errors were encountered: