Skip to content

Commit 3f047a0

Browse files
committed
fix(reactant-share): fix router init event in reactant-share
1 parent 0616f36 commit 3f047a0

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

examples/ts-counter/src/index.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useConnector,
88
action,
99
state,
10+
computed,
1011
} from 'reactant';
1112

1213
@injectable()
@@ -16,13 +17,25 @@ class Counter {
1617

1718
@action
1819
increase() {
20+
console.log('increase0', this.count, this.doubleCount, this.doubleCount1);
1921
this.count += 1;
22+
console.log('increase1', this.count, this.doubleCount, this.doubleCount1);
2023
}
2124

2225
@action
2326
decrease() {
2427
this.count -= 1;
2528
}
29+
30+
@computed
31+
get doubleCount() {
32+
return this.count * 2;
33+
}
34+
35+
@computed((that: Counter) => [that.count])
36+
get doubleCount1() {
37+
return this.count * 2;
38+
}
2639
}
2740

2841
@injectable()
@@ -33,12 +46,16 @@ class AppView extends ViewModule {
3346

3447
component() {
3548
const count = useConnector(() => this.counter.count);
49+
const doubleCount = useConnector(() => this.counter.doubleCount);
50+
const doubleCount1 = useConnector(() => this.counter.doubleCount1);
3651
return (
3752
<>
3853
<button type="button" onClick={() => this.counter.decrease()}>
3954
-
4055
</button>
41-
<div>{count}</div>
56+
<p>{count}</p>
57+
<p>{doubleCount}</p>
58+
<p>{doubleCount1}</p>
4259
<button type="button" onClick={() => this.counter.increase()}>
4360
+
4461
</button>
@@ -50,6 +67,9 @@ class AppView extends ViewModule {
5067
const app = createApp({
5168
main: AppView,
5269
render,
70+
// devOptions: {
71+
// autoComputed: true,
72+
// },
5373
});
5474

5575
app.bootstrap(document.getElementById('root'));

packages/reactant-share/src/modules/router.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ class ReactantRouter extends BaseReactantRouter {
6767
),
6868
});
6969

70+
this.portDetector.onClient(() => {
71+
const stopWatching = watch(
72+
this,
73+
() => this.portDetector.lastAction.action,
74+
() => {
75+
const action = this.portDetector.lastAction
76+
.action as any as LocationChangeAction;
77+
if (
78+
action.type === LOCATION_CHANGE &&
79+
action.payload.isFirstRendering
80+
) {
81+
const router = this._routers[this.portDetector.name];
82+
if (
83+
router &&
84+
this.history.createHref(router.location) !==
85+
this.history.createHref(this.router!.location)
86+
) {
87+
stopWatching();
88+
// router reducer @@router/LOCATION_CHANGE event and syncFullState event The events may be out of order, so we re-check route consistency after synchronizing the state.
89+
this.history.replace(router.location);
90+
}
91+
}
92+
}
93+
);
94+
});
95+
7096
if (globalThis.document) {
7197
window.addEventListener('popstate', () => {
7298
if (!this.passiveRoute) {

0 commit comments

Comments
 (0)