-
-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added useReactiveValue/s (#65)
* chore: added useReactiveVlaue/s * chore: updated basic example
- Loading branch information
Showing
6 changed files
with
96 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -481,4 +481,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: aff15ad1e9bd5c910cdc2b575487ce8c56864ae7 | ||
|
||
COCOAPODS: 1.9.1 | ||
COCOAPODS: 1.9.3 |
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
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,20 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import Animated from 'react-native-reanimated'; | ||
|
||
export const useReactiveValue = (value: number) => { | ||
// ref | ||
const ref = useRef<Animated.Value<number>>(null); | ||
if (ref.current === null) { | ||
// @ts-ignore | ||
ref.current = new Animated.Value(value); | ||
} | ||
|
||
// effects | ||
useEffect(() => { | ||
if (ref.current) { | ||
ref.current.setValue(value); | ||
} | ||
}, [value]); | ||
|
||
return ref.current; | ||
}; |
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,47 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import Animated from 'react-native-reanimated'; | ||
|
||
export const useReactiveValues = (values: number[]) => { | ||
// ref | ||
const ref = useRef<Animated.Value<number>[]>(null); | ||
if (ref.current === null) { | ||
// @ts-ignore | ||
ref.current = []; | ||
values.map(value => { | ||
// @ts-ignore | ||
ref.current.push(new Animated.Value(value)); | ||
}); | ||
} | ||
|
||
// effects | ||
useEffect(() => { | ||
if (ref.current) { | ||
values.map((value, index) => { | ||
// @ts-ignore | ||
if (ref.current[index]) { | ||
// update current value | ||
// @ts-ignore | ||
ref.current[index].setValue(value); | ||
} else { | ||
// insert current value | ||
// @ts-ignore | ||
ref.current.push(new Animated.Value(value)); | ||
} | ||
}); | ||
|
||
/** | ||
* if previous animated array has more values than the updated | ||
* array, we will need to set the extra values to the last | ||
* value of the updated array. | ||
*/ | ||
if (values.length < ref.current.length) { | ||
const lastValue = values[values.length - 1]; | ||
for (let i = values.length - 1; i <= ref.current.length - 1; i++) { | ||
ref.current[i].setValue(lastValue); | ||
} | ||
} | ||
} | ||
}, [values]); | ||
|
||
return ref.current!; | ||
}; |