Kev*_*vvv 2 d3.js reactjs react-native react-hooks
我正在尝试将 React Native 的类组件转换为涉及useRef. 以下是类组件:
import React, { Component } from "react";
import { AppRegistry, StyleSheet, Text, View, Animated, TouchableWithoutFeedback } from "react-native";
import { interpolateNumber, interpolateRgb } from "d3-interpolate";
export default class animations extends Component {
state = {
animation: new Animated.Value(0)
};
componentWillMount() {
const positionInterpolate = interpolateNumber(0, 200);
const colorInterpolate = interpolateRgb("rgb(255,99,71)", "rgb(99,71,255)");;
this.state.animation.addListener(({value}) => {
const position = positionInterpolate(value);
const color = colorInterpolate(value);
const style = [
styles.box,
{
backgroundColor: color,
transform: [
{translateY: position}
]
}
];
this._view.setNativeProps({ style });
});
}
handlePress = () => {
Animated.timing(this.state.animation, {
toValue: 1,
duration: 500,
}).start();
}
render() {
return (
<View style={styles.container}>
<TouchableWithoutFeedback onPress={this.handlePress}>
<View style={styles.box} ref={view => this._view = view} />
</TouchableWithoutFeedback>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我转换为以下功能组件时,出现以下错误:
类型错误 someRef.setNativeProps 不是函数。(在 'someRef.setNativeProps({ style: style })' 中,'someRef.setNativeProps' 未定义)
import
React,
{
useState,
useEffect,
useRef
} from 'react'
import {
View,
Animated,
TouchableWithoutFeedback,
StyleSheet
} from 'react-native'
import {
interpolateNumber,
interpolateRgb,
} from 'd3-interpolate'
export default d3number = () => {
const [animation] = useState(new Animated.Value(1))
const [position, setPosition] = useState()
const someRef = useRef(null)
const startAnimation = () => {
Animated.timing(animation, {
toValue: 2,
duration: 1500,
}).start()
}
const positionInterpolate = interpolateNumber(0, 300)
useEffect(() => {
animation.addListener(({value}) => {
const pos = positionInterpolate(value)
setPosition(pos)
})
const style = [
styles.box,
{
transform: [
{
translateY: position
}
]
}
]
someRef.setNativeProps({ style })
// console.log(someRef.setNativeProps)
}, [])
return (
<View style={styles.container}>
<TouchableWithoutFeedback onPress={startAnimation}>
<View
style={styles.box}
ref={someRef}
/>
</TouchableWithoutFeedback>
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
更新:这是通过在侦听器中包含样式来解决的
if (!someRef.current) {
return;
}
animation.addListener(({value}) => {
const style = [
styles.box,
{
transform: [
{
translateY: positionInterpolate(value)
}
]
}
]
someRef.current.setNativeProps({ style })
})
// console.log(someRef.setNativeProps)
}, [someRef.current])
Run Code Online (Sandbox Code Playgroud)
根据文档 [1] ref 的值保持在ref.current. 所以你需要运行someRef.current.setNativeProps.
那就是说您的useEffect代码会出现问题,因为someRef将在运行null时设置为您的第一次渲染useEffect。您必须添加someRef.current到useEffect依赖项数组,以便在someRef更新时调用 effect并添加条件以不调用someRef.current.setNativePropswhen someRef.currentis null。您可能想跳过整个useEffect正文,除非someRef.current不是null。
useEffect(() => {
if (!someRef.current) {
return;
}
animation.addListener(({value}) => {
const pos = positionInterpolate(value)
setPosition(pos)
})
const style = [
styles.box,
{
transform: [
{
translateY: position
}
]
}
]
someRef.current.setNativeProps({ style })
}, [someRef.current]) # note the addition of `someRef.current` here so the effect runs again when the ref is set
Run Code Online (Sandbox Code Playgroud)
[1] https://reactjs.org/docs/hooks-reference.html#useref
| 归档时间: |
|
| 查看次数: |
8800 次 |
| 最近记录: |