-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f350eb
commit a642bba
Showing
6 changed files
with
107 additions
and
21 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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Controller from '../../../controller' | ||
import useCtrl from '../../../hook/useCtrl' | ||
import React, { useState } from 'react' | ||
|
||
declare global { | ||
module NodeJS { | ||
interface Global { | ||
controller: any | ||
} | ||
} | ||
interface Window { | ||
controller: any | ||
} | ||
} | ||
|
||
export default class BatchRefreshController extends Controller { | ||
View = View | ||
constructor(location: any, context: any) { | ||
super(location, context) | ||
if (context.isClient) { | ||
window.controller = this | ||
} else if (context.isServer) { | ||
global.controller = this | ||
} | ||
} | ||
initialState = { | ||
count: 0 | ||
} | ||
// 选择是否禁用防抖刷新 | ||
disableBatchRefresh: boolean = true | ||
handleIncre = () => { | ||
setTimeout(() => { | ||
// 重复调用多次,只会触发一次更新 | ||
this.store.actions.UPDATE_STATE({ | ||
count: this.store.getState().count + 1 | ||
}) | ||
this.store.actions.UPDATE_STATE({ | ||
count: this.store.getState().count + 1 | ||
}) | ||
}, 0) | ||
} | ||
} | ||
|
||
|
||
function View({ state }: any) { | ||
const ctrl = useCtrl() | ||
const [count, setCount] = useState(0) | ||
|
||
const handleIncre = () => { | ||
setTimeout(() => { | ||
setCount(count => count + 1) | ||
setCount(count => count + 1) | ||
}, 0) | ||
} | ||
|
||
|
||
console.log('render', state.count, count) | ||
return ( | ||
<div id="debounce-refresh"> | ||
<div id="count">{state.count}</div> | ||
<button id="ctrl-incre" onClick={ctrl.handleIncre}> | ||
ctrl.incre | ||
</button> | ||
<button id="state-incre" onClick={handleIncre}> | ||
state.incre | ||
</button> | ||
</div> | ||
) | ||
} |
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