Skip to content

Commit a51f015

Browse files
docs: added example expo snack
1 parent 22a9b27 commit a51f015

File tree

1 file changed

+17
-81
lines changed

1 file changed

+17
-81
lines changed

README.md

Lines changed: 17 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,36 @@ Abstraction for `React Native`'s `LayoutAnimation` and `useState`
33

44
## Example
55

6-
```tsx
7-
// This example is taken from the React Native documentation, with a little bit of modification.
8-
import React from 'react';
9-
import {View, StyleSheet, TouchableOpacity} from 'react-native';
10-
import useStateWithLayoutAnimation from 'use-state-with-layout-animation';
11-
12-
const App = () => {
13-
const [state, setState] = useStateWithLayoutAnimation({w: 100, h: 100});
14-
15-
const spring = () => {
16-
setState.spring({w: state.w + 15, h: state.h + 15});
17-
};
18-
19-
const linear = () => {
20-
setState.linear({w: state.w + 15, h: state.h + 15});
21-
};
22-
23-
const easeInEaseOut = () => {
24-
setState.easeInEaseOut({w: state.w + 15, h: state.h + 15});
25-
};
26-
27-
const reset = () => {
28-
setState.noAnimation({w: 100, h: 100});
29-
};
30-
31-
return (
32-
<View style={styles.container}>
33-
<View style={[styles.box, {width: state.w, height: state.h}]} />
34-
<TouchableOpacity onPress={spring}>
35-
<View style={styles.button}>
36-
<Text style={styles.buttonText}>Spring</Text>
37-
</View>
38-
</TouchableOpacity>
39-
<TouchableOpacity onPress={linear}>
40-
<View style={styles.button}>
41-
<Text style={styles.buttonText}>Linear</Text>
42-
</View>
43-
</TouchableOpacity>
44-
<TouchableOpacity onPress={easeInEaseOut}>
45-
<View style={styles.button}>
46-
<Text style={styles.buttonText}>Ease in, Ease out</Text>
47-
</View>
48-
</TouchableOpacity>
49-
<TouchableOpacity onPress={reset}>
50-
<View style={styles.button}>
51-
<Text style={styles.buttonText}>Reset</Text>
52-
</View>
53-
</TouchableOpacity>
54-
</View>
55-
);
56-
};
57-
58-
export default App;
59-
60-
const styles = StyleSheet.create({
61-
box: {
62-
backgroundColor: 'tomato',
63-
height: 200,
64-
width: 200,
65-
},
66-
button: {
67-
backgroundColor: 'black',
68-
marginTop: 15,
69-
paddingHorizontal: 20,
70-
paddingVertical: 15,
71-
},
72-
buttonText: {
73-
color: '#fff',
74-
fontWeight: 'bold',
75-
},
76-
container: {
77-
alignItems: 'center',
78-
flex: 1,
79-
justifyContent: 'center',
80-
},
81-
});
82-
```
6+
[Scan the QR code and run the snack on your `iOS` or `Android` device](https://snack.expo.io/@iamkarlmarx/usestatewithlayoutanimation). (It does not work on web)
837

848
## API
9+
10+
### `useStateWithLayoutAnimation`
8511
By default, `UIManager.setLayoutAnimationEnabledExperimental` is invoked, you can pass `false` as the second parameter if you want to call it on your own.
8612
```
87-
useStateWithLayoutAnimation(initialState: any, setLayoutAnimationEnabledExperimental?: boolean = true)
13+
const [state, setState] = useStateWithLayoutAnimation(123, false);
8814
```
89-
90-
You can pass a callback as second parameter to `setState` animation functions to be called when animation is finished
15+
### `setState.spring`
16+
### `setState.linear`
17+
### `setState.easeInEaseOut`
18+
You can use this the same as `useState` setter, accepts values or optional callback function but accepts a second parameter for the animation finish callback.
9119
```ts
9220
const [state, setState] = useStateWithLayoutAnimation(1);
9321

9422
const animationDidFinish = () => console.log('Animation finished');
9523

9624
setState.spring(2, animationDidFinish);
97-
setState.linear(3, animationDidFinish);
25+
setState.linear(prev => prev + 10, animationDidFinish);
9826
setState.easeInEaseOut(4, animationDidFinish);
9927
```
10028

29+
### `setState.noAnimation`
30+
You can use this the same as `useState` setter, accepts values or optional callback function.
31+
```ts
32+
setState.noAnimation(4);
33+
setState.noAnimation(prev => prev + 1);
34+
```
35+
36+
10137
## License
10238
[MIT](https://github.com/karlmarxlopez/useStateWithLayoutAnimation/blob/master/LICENSE)

0 commit comments

Comments
 (0)