这应该可以,希望它有帮助:)
您需要一种方法来跟踪屏幕之间的状态。这样你就知道在屏幕 B 中,在屏幕 A 中按下了按钮。只需使用 React 的 useContext 即可。(https://dmitripavlutin.com/react-context-and-usecontext/)
创建一个名为 DataLayer.js 的文件
import React from "react";
const DataLayerContext = React.createContext();
const DataLayerProvider = (props) => {
const [showAlertInScreenB, setShowAlertInScreenB] = React.useState(false);
return (
<DataLayerContext.Provider value={{
showAlertInScreenB, setShowAlertInScreenB
}}
>
</DataLayerContext.Provider>
)
}
export { DataLayerProvider, DataLayerContext }
Next 您需要在单击按钮时将屏幕 A 中的 showAlertInScreenB 设置为 true。所以在屏幕 A 中执行此操作。
`
import { DataLayerContext } from "./DataLayer";
const ScreenA = (props)=>{
const DATA_LAYER = React.useContext(DataLayerContext)
const taskedClick = () => {
DATA_LAYER.setShowAlertInScreenB(true);
}
return (
<View>
<Button onPress={taskedClick}
style={styles.leftButton}
type="solid"
title={post.leftUser.leftButton}
/>
</View>
}
`
最后,每当您进入屏幕 A 时,您都会根据数据层中的值 showAlertInScreenB 有条件地显示警报
import { DataLayerContext } from "./DataLayer";
const ScreenA = (props)=>{
const DATA_LAYER = React.useContext(DataLayerContext)
if (DATA_LAYER.showAlertInScreenB===true){
Alert.alert(
"Alert Title",
"My Alert Msg",
[
{
text: "Cancel",
onPress: () => {DATA_LAYER.setShowAlertInScreenB(false)},
style: "cancel"
},
{ text: "OK", onPress: () =>{DATA_LAYER.setShowAlertInScreenB(false) }
]
);
}
}