Comments (3)
I'm glad that worked. I assume it didn't work because by having the component definition inside, React always thought it was a new component and destroyed and recreated it. After recreating it has no idea it should focus it too, hence your problem. When you create a component it's like creating a class in Scala. You only very, very rarely create a new class inside a function/method; normally the class is defined elsewhere and you only create a new instance of it in a function.
from scalajs-react.
Hi. I think should align it a bit more with the JS example, it'll work. So move the declaration of Example
out from the inside of Examples
, and pass in the onChange callback through properties. That's probably all you need.
from scalajs-react.
Hmmm... indeed it works with your suggested change. I am not sure, however, why that made a difference. It will be great if you can explain so that I can apply the same refactoring to similar components in my real project. Thanks!
Here is my refactored main()
, in case someone comes here looking:
def main(): Unit = {
val Example = ReactComponentB[(String, SyntheticEvent[HTMLInputElement] => Unit)]("Example")
.render{ props =>
val (v, change) = props
input(value := v, onchange ==> change)
}.build
val Examples = ReactComponentB[Unit]("examples")
.initialState(IndexedSeq("first", "second"))
.backend(new ExampleBackend(_))
.render((_,state,backend) => {
val Elements = state.zipWithIndex.map{case(v, index) => {
Example.withKey(s"key$index")((v, backend.onChange(index)))
}}
div(Elements)
}
).build(())
React.renderComponent(Examples, dom.document.getElementById("playground"))
}
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.