第一个我想在父组件中得到更新的currentChildIndex。它在第一次加载时显示索引,但之后不会更新。尝试查找useState钩子,但没有找到
currentChildIndex
nhaq1z211#
您只需要传递setCurrentChildIndex即可:
setCurrentChildIndex
const Parent = () => { const [currentChildIndex, setCurrentChildIndex] = useState(); useEffect(() => { console.log("CurrentChildIndex has been updated", currentChildIndex); }, [currentChildIndex]); return <Child setCurrentChildIndex={setCurrentChildIndex} />; };
并在Child组件而不是setIndex中使用它
Child
setIndex
const Child =({setCurrentChildIndex})=>{ <FlatList contentContainerStyle={styles.contentContainer} ref={flatListRef} scrollEnabled showsVerticalScrollIndicator={false} data={videos} onMomentumScrollEnd={e => { setCurrentChildIndex(Math.round(e.nativeEvent.contentOffset.x / width)); }} onEndReachedThreshold={Platform.OS === 'ios' ? 0 : 1} /> /> }
1条答案
按热度按时间nhaq1z211#
您只需要传递
setCurrentChildIndex
即可:并在
Child
组件而不是setIndex
中使用它