作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 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>
);
}
}
当我转换为以下功能组件时,出现以下错误:
TypeError that someRef.setNativeProps is not a function. (In 'someRef.setNativeProps({ style: style })', 'someRef.setNativeProps' is undefined)
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>
)
}
更新:通过在监听器中包含样式解决了这个问题
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])
最佳答案
根据文档 [1],ref 的值保持在 ref.current
。所以你需要运行 someRef.current.setNativeProps
。
也就是说你的 useEffect
代码会有问题,因为当 useEffect
正在运行。您必须将 someRef.current
添加到 useEffect
依赖项数组,以便在 someRef
更新时调用效果并添加不调用的条件someRef.current.setNativeProps
当 someRef.current
为 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
关于reactjs - 如何在 React Native 中为 setNativeProps 使用 useRef 钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57876152/
我是一名优秀的程序员,十分优秀!