Skip to content

Commit

Permalink
提交ReactUseDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
张浩 committed Apr 16, 2024
1 parent 55cfa29 commit fed4b23
Show file tree
Hide file tree
Showing 69 changed files with 1,511 additions and 1,342 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/RNOHDCS.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 ReactUseDemo/CreateMemo.tsx → ReactUseDemo/Demo/CreateMemo.tsx
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
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
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
Loading

0 comments on commit fed4b23

Please sign in to comment.