Skip to content

Commit 68758c8

Browse files
committed
Fixed bug that a Binder returned from useState retains wrong value
1 parent f88a181 commit 68758c8

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Sources/Hooks/Hook/UseState.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ private struct StateHook<State>: Hook {
2222

2323
func makeValue(coordinator: Coordinator) -> Binding<State> {
2424
Binding(
25-
get: { [state = coordinator.state.state] in
26-
state
25+
get: {
26+
coordinator.state.state
2727
},
2828
set: { newState in
2929
assertMainThread()

Tests/HooksTestingTests/HookTesterTests.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ final class HookTesterTests: XCTestCase {
1717
}
1818

1919
func testValueHistory() {
20-
let tester = HookTester {
21-
useState(0)
20+
let tester = HookTester(0) { value in
21+
useMemo(.always) {
22+
value
23+
}
2224
}
2325

24-
tester.value.wrappedValue = 1
25-
tester.value.wrappedValue = 2
26-
tester.value.wrappedValue = 3
26+
tester.update(with: 1)
27+
tester.update(with: 2)
28+
tester.update(with: 3)
2729

28-
XCTAssertEqual(
29-
tester.valueHistory.map(\.wrappedValue),
30-
[0, 1, 2, 3]
31-
)
30+
XCTAssertEqual(tester.valueHistory, [0, 1, 2, 3])
3231
}
3332

3433
func testUpdateWithParameter() {

0 commit comments

Comments
 (0)