Comments (9)
What this does is configure shouldComponentUpdate
to compare equality of props & state.
Concept is good, JS implementation isn't what we want for Scala.
Better would be to add something applicable via ReactComponentB.configure
that uses scalaz.Equal
for comparison, or plain ==
for dangerous people.
from scalajs-react.
Bloody callbacks stuff up Props equality... Might need to introduce a mechanism for callback identity and comparison.
from scalajs-react.
+1 First thing i was sad about.
from scalajs-react.
A better equivalent of this will be in the next version of scalajs-react.
(Better because the JS version 1) has an issue, 2) doesn't play with with Scala, 3) give no guarantees).
Release should be out in under a month.
from scalajs-react.
Awesome!
from scalajs-react.
Right now i'm using this workaround.
implicit class ReactCompBExt[Props, State, Backend](comp: ReactComponentB[Props, State, Backend]) {
def pure = shouldUpdateS { case (was, toBe) => was != toBe }
def shouldUpdate(f: ((Props, Props)) => Boolean) = comp.shouldComponentUpdate(
(scope, newProps, _) => f(scope.props, newProps))
def shouldUpdateS(f: (((Props, State), (Props, State))) => Boolean) = comp.shouldComponentUpdate(
(scope, newProps, newState) => f((scope.props, scope.state), (newProps, newState))
)
}
I could say nothing about performance yet though
from scalajs-react.
Using universal equality is dangerous in that when you, maybe now, maybe later, slip something in your props or state for which ==
doesn't hold, you're not going to know unless you pay close attention at runtime and then investigate back. The Scala compiler is not your friend and will not help you.
This is especially an issue when you pass callbacks around (which is the norm). How would one expect Scala to compare functions? By reference? Most callbacks will be regenerated unless you've structured them a very special way, and again, because the compiler's not involved there's no way of knowing whether you're doing it wrong and are going to break shouldComponentUpdate
on every call.
It's unpleasant but it's a real issue. It's something to be aware of when using an approach like above.
I have a solution for these issues. I'll try to get it online well before the release so they can be critiqued. Feedback is always appreciated and just now looking at your snippet there's already a case I didn't touch - state comparison (I only use props comparison).
from scalajs-react.
I've created a branch for the performance management stuff that will go into 0.9.0 and added tools that address this issue.
Read all about it here:
https://github.com/japgolly/scalajs-react/blob/0.9/extra/PERF.md
This will go out in 0.9.0 in about a month's time, and close this issue.
from scalajs-react.
Closed via 9e8f5bd
from scalajs-react.
Related Issues (20)
- Export `assertOuterHTML` HOT 1
- Deprecate `Mounted` component types HOT 2
- Add default arg to `ReactTestUtils.withRendered` HOT 1
- ScalaFnComponent is anonymous HOT 2
- Consider using `DocumentFragments` in `ReactTestUtils` HOT 1
- Re-evaluate `ComponentDom` post- React 18
- Is there a way to integrate Components with cats.effect.Ref? HOT 1
- Ref not always set HOT 2
- Syntax to integrate `useContext` with context function in Scala 3 HOT 1
- Compilation fails in Scala 3 when deriving Reusability for case classes with multiple parameter groups HOT 1
- Missing getDerivedStateFromError
- Missing / misspelled packages in MODULES.md HOT 1
- Add support for Discussions tab in Github HOT 1
- Modern testing recipe?
- Cannot mix static and dynamic routes even if conditional
- A minimal setup fails with Scala 3 HOT 5
- Provide default IO/SyncIO error handler for component
- ScalaFnComponent doesn't include displayName HOT 1
- Another way to define hook components
- AsyncCallback + cats, gracefully handling errors with EitherT: unexpected behaviour HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scalajs-react.