2 回答

TA貢獻1840條經驗 獲得超5個贊
以下是一些建議:
1. AsyncStorage:您可以將它們保存到 AsyncStorage(這是手機上硬件支持的安全存儲),并且可以隨時檢索它們。假設用戶的日程安排是這些事件的數組,例如:
var a = [{ name: eventName, time: eventTime }, { name: eventName, time: eventTime }]
你可以這樣做:
AsyncStorage.setItem('Schedule', JSON.stringify(a));
AsyncStorage.getItem('Schedule');
見:https ://github.com/react-native-community/async-storage
2.后端
將事件保存到后端服務器并從其他頁面檢索它們。
3. Redux 或 React Hooks(高級) https://medium.com/swlh/react-global-state-with-hooks-f163e49f90f9 https://redux.js.org/introduction/getting-started/
4. 帶 React Hooks 的全局狀態
我最喜歡的使用內置 React Hooks 管理全局狀態的新方法

TA貢獻1856條經驗 獲得超17個贊
您可以擁有一個全局對象來存儲屏幕之間的共享數據。即使您閱讀Redux文檔,redux 的使用案例之一是當您有一些數據要在不同屏幕之間共享時。
所以這是全局對象的代碼(單例模式)
export class GlobalState {
public static getInstance(): GlobalState {
if (GlobalState.instance == null) {
GlobalState.instance = new GlobalState()
}
return GlobalState.instance
}
private state: IGlobalState = this.restoreDefaultState()
private restoreDefaultState(): IGlobalState {
return (
{
// your properties are placed here
}
)
}
}
用法:
GlobalState.getInstance() ... //call any function placed there
注意:上面的代碼是TypeScript用JS.
添加回答
舉報