forked from react-native-oh-library/RNOHDCS
-
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.
- Loading branch information
张浩
committed
Apr 16, 2024
1 parent
55cfa29
commit fed4b23
Showing
69 changed files
with
1,511 additions
and
1,342 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
59 changes: 29 additions & 30 deletions
59
ReactUseDemo/CreateGlobalState.tsx → ReactUseDemo/Demo/CreateGlobalState.tsx
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,29 @@ | ||
import React from 'react'; | ||
import { View, Text, Button } from 'react-native'; | ||
import { createGlobalState } from 'react-use' | ||
|
||
const useGlobalValue = createGlobalState<number>(0); | ||
|
||
const CompA = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <Button title='+' onPress={() => setValue(value + 1)} /> | ||
}; | ||
|
||
const CompB = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <Button title='-' onPress={() => setValue(value - 1)} /> | ||
}; | ||
|
||
const CreateGlobalStateDemo = () => { | ||
const [value] = useGlobalValue(); | ||
return ( | ||
<View> | ||
<Text>{value}</Text> | ||
<CompA /> | ||
<CompB /> | ||
</View> | ||
); | ||
}; | ||
|
||
export default CreateGlobalStateDemo; | ||
import React from 'react'; | ||
import { View, Text, Button } from 'react-native'; | ||
import { createGlobalState } from 'react-use' | ||
|
||
const useGlobalValue = createGlobalState<number>(0); | ||
|
||
const CompA = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <Button title='+' onPress={() => setValue(value + 1)} /> | ||
}; | ||
|
||
const CompB = () => { | ||
const [value, setValue] = useGlobalValue(); | ||
|
||
return <Button title='-' onPress={() => setValue(value - 1)} /> | ||
}; | ||
|
||
const CreateGlobalStateDemo = () => { | ||
const [value] = useGlobalValue(); | ||
return ( | ||
<View> | ||
<Text>{value}</Text> | ||
<CompA /> | ||
<CompB /> | ||
</View> | ||
); | ||
}; | ||
export default CreateGlobalStateDemo |
45 changes: 22 additions & 23 deletions
45
ReactUseDemo/CreateMemo.tsx → ReactUseDemo/Demo/CreateMemo.tsx
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,23 +1,22 @@ | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
import { createMemo } from 'react-use'; | ||
|
||
const fibonacci: any = n => { | ||
if (n === 0) return 0; | ||
if (n === 1) return 1; | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
}; | ||
|
||
const useMemoFibonacci = createMemo(fibonacci); | ||
|
||
const CreateMemoDemo = () => { | ||
const result = useMemoFibonacci(10); | ||
|
||
return ( | ||
<Text> | ||
fib(10) = {result} | ||
</Text> | ||
); | ||
}; | ||
|
||
export default CreateMemoDemo; | ||
import React from 'react'; | ||
import { Text } from 'react-native'; | ||
import { createMemo } from 'react-use'; | ||
|
||
const fibonacci: any = (n: number) => { | ||
if (n === 0) return 0; | ||
if (n === 1) return 1; | ||
return fibonacci(n - 1) + fibonacci(n - 2); | ||
}; | ||
|
||
const useMemoFibonacci = createMemo(fibonacci); | ||
|
||
const CreateMemoDemo = () => { | ||
const result = useMemoFibonacci(10); | ||
|
||
return ( | ||
<Text> | ||
fib(10) = {result} | ||
</Text> | ||
); | ||
}; | ||
export default CreateMemoDemo |
107 changes: 53 additions & 54 deletions
107
ReactUseDemo/CreateReducerContext.tsx → ReactUseDemo/Demo/CreateReducerContext.tsx
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,54 +1,53 @@ | ||
import React from 'react'; | ||
import { View, Button, Text } from 'react-native'; | ||
import { createReducerContext } from 'react-use'; | ||
|
||
type Action = 'increment' | 'decrement'; | ||
|
||
const reducer = (state: number, action: Action) => { | ||
switch (action) { | ||
case 'increment': | ||
return state + 1; | ||
case 'decrement': | ||
return state - 1; | ||
default: | ||
throw new Error(); | ||
} | ||
}; | ||
|
||
const [useSharedCounter, SharedCounterProvider] = createReducerContext(reducer, 0); | ||
|
||
const ComponentA = () => { | ||
const [count, dispatch] = useSharedCounter(); | ||
return ( | ||
<View> | ||
<Text>Component A</Text> | ||
<Button title='-' onPress={() => dispatch('decrement')} /> | ||
<Text>{count}</Text> | ||
<Button title='+' onPress={() => dispatch('increment')} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const ComponentB = () => { | ||
const [count, dispatch] = useSharedCounter(); | ||
return ( | ||
<View> | ||
<Text>Component B</Text> | ||
<Button title='-' onPress={() => dispatch('decrement')} /> | ||
<Text>{count}</Text> | ||
<Button title='+' onPress={() => dispatch('increment')} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const CreateReducerContextDemo = () => { | ||
return ( | ||
<SharedCounterProvider> | ||
<Text>Those two counters share the same value.</Text> | ||
<ComponentA /> | ||
<ComponentB /> | ||
</SharedCounterProvider> | ||
); | ||
}; | ||
|
||
export default CreateReducerContextDemo; | ||
import React from 'react'; | ||
import { View, Button, Text } from 'react-native'; | ||
import { createReducerContext } from 'react-use'; | ||
|
||
type Action = 'increment' | 'decrement'; | ||
|
||
const reducer = (state: number, action: Action) => { | ||
switch (action) { | ||
case 'increment': | ||
return state + 1; | ||
case 'decrement': | ||
return state - 1; | ||
default: | ||
throw new Error(); | ||
} | ||
}; | ||
|
||
const [useSharedCounter, SharedCounterProvider] = createReducerContext(reducer, 0); | ||
|
||
const ComponentA = () => { | ||
const [count, dispatch] = useSharedCounter(); | ||
return ( | ||
<View> | ||
<Text>Component A</Text> | ||
<Button title='-' onPress={() => dispatch('decrement')} /> | ||
<Text>{count}</Text> | ||
<Button title='+' onPress={() => dispatch('increment')} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const ComponentB = () => { | ||
const [count, dispatch] = useSharedCounter(); | ||
return ( | ||
<View> | ||
<Text>Component B</Text> | ||
<Button title='-' onPress={() => dispatch('decrement')} /> | ||
<Text>{count}</Text> | ||
<Button title='+' onPress={() => dispatch('increment')} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const CreateReducerContextDemo = () => { | ||
return ( | ||
<SharedCounterProvider> | ||
<Text>Those two counters share the same value.</Text> | ||
<ComponentA /> | ||
<ComponentB /> | ||
</SharedCounterProvider> | ||
); | ||
}; | ||
export default CreateReducerContextDemo |
74 changes: 37 additions & 37 deletions
74
ReactUseDemo/CreateStateContext.tsx → ReactUseDemo/Demo/CreateStateContext.tsx
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,37 +1,37 @@ | ||
import React from 'react'; | ||
import { View, TextInput, Text } from 'react-native'; | ||
import { createStateContext } from 'react-use'; | ||
|
||
const [useSharedText, SharedTextProvider] = createStateContext(''); | ||
|
||
const ComponentA = () => { | ||
const [text, setText] = useSharedText(); | ||
return ( | ||
<View> | ||
<Text>Component A:</Text> | ||
<TextInput style={{borderWidth:1}} value={text} onChangeText={value => setText(value)} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const ComponentB = () => { | ||
const [text, setText] = useSharedText(); | ||
return ( | ||
<View> | ||
<Text>Component B:</Text> | ||
<TextInput style={{borderWidth:1}} value={text} onChangeText={value => setText(value)} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const CreateStateContextDemo = () => { | ||
return ( | ||
<SharedTextProvider> | ||
<Text>Those two fields share the same value.</Text> | ||
<ComponentA /> | ||
<ComponentB /> | ||
</SharedTextProvider> | ||
); | ||
}; | ||
|
||
export default CreateStateContextDemo; | ||
import React from 'react'; | ||
import { View, TextInput, Text } from 'react-native'; | ||
import { createStateContext } from 'react-use'; | ||
|
||
const [useSharedText, SharedTextProvider] = createStateContext(''); | ||
|
||
const ComponentA = () => { | ||
const [text, setText] = useSharedText(); | ||
return ( | ||
<View> | ||
<Text>Component A:</Text> | ||
<TextInput style={{borderWidth:1}} value={text} onChangeText={value => setText(value)} /> | ||
</View> | ||
); | ||
}; | ||
|
||
const ComponentB = () => { | ||
const [text, setText] = useSharedText(); | ||
return ( | ||
<View> | ||
<Text>Component B:</Text> | ||
<Text style={{borderWidth:1}}>{text}</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
const CreateStateContextDemo = () => { | ||
return ( | ||
<SharedTextProvider> | ||
<Text>A组件和B组件单向共享状态</Text> | ||
<ComponentA /> | ||
<ComponentB /> | ||
</SharedTextProvider> | ||
); | ||
}; | ||
|
||
export default CreateStateContextDemo |
Oops, something went wrong.