forked from alibaba/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request alibaba#808 from Turkyden/fix/useReactive
fix/useReactive: add the usage demo of computed properties
- Loading branch information
Showing
6 changed files
with
81 additions
and
29 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 |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { useReactive } from 'ahooks'; | ||
|
||
export default () => { | ||
const state = useReactive({ count: 0 }); | ||
const [stateCount, setStateCount] = useState(0); | ||
|
||
const state2 = useReactive({ count: 0 }); | ||
const [stateCount2, setStateCount2] = useState(0); | ||
|
||
// 依赖的是对象,因为始终同一个引用,所以不会执行 | ||
useEffect(() => { | ||
setStateCount(stateCount + 1); | ||
}, [state]); | ||
|
||
// 依赖的基础数据类型,所以只要改变就会重新执行 | ||
useEffect(() => { | ||
setStateCount2(stateCount2 + 1); | ||
}, [state2.count]); | ||
const state = useReactive({ | ||
bug: '', | ||
bugs: ['feat', 'fix', 'chore'], | ||
addBug(bug) { | ||
this.bugs.push(bug); | ||
}, | ||
get bugsCount() { | ||
return this.bugs.length; | ||
}, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<button style={{ marginTop: 20 }} onClick={() => (state.count += 1)}> | ||
stateCount + 1 | ||
</button> | ||
<p>stateCount:{stateCount}</p> | ||
<p>state.bugsCount: {state.bugsCount}</p> | ||
|
||
<form | ||
onSubmit={(e) => { | ||
state.addBug(state.bug); | ||
state.bug = ''; | ||
e.preventDefault(); | ||
}} | ||
> | ||
<input type="text" value={state.bug} onChange={(e) => (state.bug = e.target.value)} /> | ||
<button type="submit" style={{ marginLeft: '10px' }}> | ||
Add | ||
</button> | ||
<button type="button" style={{ marginLeft: '10px' }} onClick={() => state.bugs.pop()}> | ||
Delete | ||
</button> | ||
</form> | ||
|
||
<br /> | ||
|
||
<button style={{ marginTop: 20 }} onClick={() => (state2.count += 1)}> | ||
stateCount2 + 1 | ||
</button> | ||
<p>stateCount2:{stateCount2}</p> | ||
<ul> | ||
{state.bugs.map((bug) => ( | ||
<li key={bug}>{bug}</li> | ||
))} | ||
</ul> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useReactive } from 'ahooks'; | ||
|
||
export default () => { | ||
const state = useReactive({ count: 0 }); | ||
const [stateCount, setStateCount] = useState(0); | ||
|
||
const state2 = useReactive({ count: 0 }); | ||
const [stateCount2, setStateCount2] = useState(0); | ||
|
||
// 依赖的是对象,因为始终同一个引用,所以不会执行 | ||
useEffect(() => { | ||
setStateCount(stateCount + 1); | ||
}, [state]); | ||
|
||
// 依赖的基础数据类型,所以只要改变就会重新执行 | ||
useEffect(() => { | ||
setStateCount2(stateCount2 + 1); | ||
}, [state2.count]); | ||
|
||
return ( | ||
<div> | ||
<button style={{ marginTop: 20 }} onClick={() => (state.count += 1)}> | ||
stateCount + 1 | ||
</button> | ||
<p>stateCount:{stateCount}</p> | ||
|
||
<button style={{ marginTop: 20 }} onClick={() => (state2.count += 1)}> | ||
stateCount2 + 1 | ||
</button> | ||
<p>stateCount2:{stateCount2}</p> | ||
</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