-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vdom): change
shouldUpdate
API for stateless components
This API is much more flexible and opens up different possibilities how to improve stateless components without breaking API in the future. The key problem with such API was that there is no direct way to get access to the stateless component descriptor from the factory function, but the trick here is to execute factory function and retrieve component descriptor from the virtual DOM node. BREAKING CHANGE: `shouldUpdate` for stateless components is now assigned with a `withShouldUpdate()` function. Before: const C = statelessComponent( (props) => h.div().c(props.title), (prev, next) => (prev.title !== next.title), ); After: const C = withShouldUpdate( (prev, next) => (prev.title !== next.title), statelessComponent( (props) => h.div().c(props.title), ), );
- Loading branch information
Showing
5 changed files
with
81 additions
and
50 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
import { statelessComponent } from "ivi"; | ||
import { statelessComponent, withShouldUpdate } from "ivi"; | ||
import * as h from "ivi-html"; | ||
import { startRender } from "./utils"; | ||
|
||
test(`props should be passed to render hook`, () => { | ||
startRender((r) => { | ||
let a = -1; | ||
const c = statelessComponent<number>( | ||
(props) => { | ||
expect(props).toBe(1337); | ||
a = props; | ||
return h.div().c(props); | ||
}, | ||
); | ||
|
||
r(c(1337)); | ||
|
||
expect(a).toBe(1337); | ||
}); | ||
}); | ||
|
||
test(`props should be passed to shouldUpdate hook`, () => { | ||
startRender((r) => { | ||
const c = statelessComponent<number>( | ||
(props) => h.div().c(props), | ||
let a = -1; | ||
let b = -1; | ||
|
||
const c = withShouldUpdate<number>( | ||
(oldProps, newProps) => { | ||
expect(oldProps).toBe(1337); | ||
expect(newProps).toBe(1338); | ||
a = oldProps; | ||
b = newProps; | ||
return true; | ||
}, | ||
statelessComponent<number>( | ||
(props) => h.div().c(props), | ||
), | ||
); | ||
|
||
r(c(1337)); | ||
r(c(1338)); | ||
|
||
expect(a).toBe(1337); | ||
expect(b).toBe(1338); | ||
}); | ||
}); |
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
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
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