无法访问 onPanResponderMove 内更新的状态或道具
Cannot access updated state or props inside onPanResponderMove
我想做什么:更改状态以响应用户的 up/down 滑动。
环境:React native 0.61.5
我有如下组件。
import React, { useRef, useState } from 'react';
import { View, PanResponder } from 'react-native';
function SimpleWrapper(props) {
const { children } = props;
const [abc, setabc] = useState(0);
console.log('SimpleWrapper', abc);
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onStartShouldSetPanResponderCapture: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
onMoveShouldSetPanResponder: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onMoveShouldSetPanResponderCapture: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onPanResponderGrant: (evt, gestureState) => console.log('onPanResponderGrant', gestureState),
onPanResponderMove: (evt, gestureState) => handleonPanResponderMove(evt, gestureState),
onPanResponderTerminationRequest: () => {}
// onPanResponderRelease: (evt, gestureState) => handlePanResponderRelease(evt, gestureState)
})
).current;
const handleShouldSetPanResponder = (evt, gestureState) => {
const { dy } = gestureState;
const isvalid = evt.nativeEvent.touches.length === 1 && Math.abs(dy) > gestureConfig.gestureIsClickThreshold;
return isvalid;
};
const handleonPanResponderMove = (evt, gestureState) => {
const { dy, vy } = gestureState;
console.log('in handleonPanResponderMove:', abc, gestureState);
if (dy < 0) {
setabc(abc + 1);
} else if (dy > 0) {
setabc(abc - 1);
}
// }
};
return (
<View {...panResponder.panHandlers} style={}>
{children}
</View>
);
}
export default SimpleWrapper;
基本上我想increment/decrement onMoveShouldSetPanResponder里面的状态参数,
现在,
在第一次触发 onMoveShouldSetPanResponder 时,'abc' 成功地 incremented/decremented。组件中最顶层的控制台也显示新值。
但是,
在 all 连续触发之后,onMoveShouldSetPanResponder 函数内的控制台始终将 abc 打印为 0(即初始值)。它永远不会改变。
是不是我们无法访问onMoveShouldSetPanResponder 内部的更新状态?我是不是遗漏了什么或者有其他方法可以做到这一点吗?
abc 变量是一个 stale closure 您可以将回调传递给状态 setter:setabc(currentValue=>currentValue + 1);
我想做什么:更改状态以响应用户的 up/down 滑动。 环境:React native 0.61.5
我有如下组件。
import React, { useRef, useState } from 'react';
import { View, PanResponder } from 'react-native';
function SimpleWrapper(props) {
const { children } = props;
const [abc, setabc] = useState(0);
console.log('SimpleWrapper', abc);
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onStartShouldSetPanResponderCapture: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
onMoveShouldSetPanResponder: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onMoveShouldSetPanResponderCapture: (evt, gestureState) => handleShouldSetPanResponder(evt, gestureState),
// onPanResponderGrant: (evt, gestureState) => console.log('onPanResponderGrant', gestureState),
onPanResponderMove: (evt, gestureState) => handleonPanResponderMove(evt, gestureState),
onPanResponderTerminationRequest: () => {}
// onPanResponderRelease: (evt, gestureState) => handlePanResponderRelease(evt, gestureState)
})
).current;
const handleShouldSetPanResponder = (evt, gestureState) => {
const { dy } = gestureState;
const isvalid = evt.nativeEvent.touches.length === 1 && Math.abs(dy) > gestureConfig.gestureIsClickThreshold;
return isvalid;
};
const handleonPanResponderMove = (evt, gestureState) => {
const { dy, vy } = gestureState;
console.log('in handleonPanResponderMove:', abc, gestureState);
if (dy < 0) {
setabc(abc + 1);
} else if (dy > 0) {
setabc(abc - 1);
}
// }
};
return (
<View {...panResponder.panHandlers} style={}>
{children}
</View>
);
}
export default SimpleWrapper;
基本上我想increment/decrement onMoveShouldSetPanResponder里面的状态参数,
现在, 在第一次触发 onMoveShouldSetPanResponder 时,'abc' 成功地 incremented/decremented。组件中最顶层的控制台也显示新值。
但是, 在 all 连续触发之后,onMoveShouldSetPanResponder 函数内的控制台始终将 abc 打印为 0(即初始值)。它永远不会改变。
是不是我们无法访问onMoveShouldSetPanResponder 内部的更新状态?我是不是遗漏了什么或者有其他方法可以做到这一点吗?
abc 变量是一个 stale closure 您可以将回调传递给状态 setter:setabc(currentValue=>currentValue + 1);