React native 对所有屏幕响应本机导航传递数据库变量
我使用带有“React Navigation 5”的“WatermelonDb”从所有应用程序屏幕全局访问数据库变量 根据我对在导航中将初始变量传递到屏幕的理解,我们使用“initialParams” 但是,当我这样做时,我会得到一个错误:React native 对所有屏幕响应本机导航传递数据库变量,react-native,stack-navigator,watermelondb,React Native,Stack Navigator,Watermelondb,我使用带有“React Navigation 5”的“WatermelonDb”从所有应用程序屏幕全局访问数据库变量 根据我对在导航中将初始变量传递到屏幕的理解,我们使用“initialParams” 但是,当我这样做时,我会得到一个错误: TypeError: Converting circular structure to JSON --> starting at object with constructor 'Database' | property 'co
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Database'
| property 'collections' -> object with constructor 'CollectionMap'
| property 'map' -> object with constructor 'Object'
| property 'alarms' -> object with constructor 'Collection'
--- property 'database' closes the circle
是否有其他方法可以全局访问数据库变量
下面是我试图将数据库变量传递给intialParams的代码部分
render()
{
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="HomeScreen">
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
initialParams={{db:database} } // <-
options={{title: 'Welcome',headerShown: false}}
/>
<Stack.Screen name="AlarmDetails" options={{title: 'Welcome',headerShown: false}} component={AlarmDetails} />
</Stack.Navigator>
</NavigationContainer>
)
render()
{
报税表(
似乎将不可序列化的对象传递到initialParams
中不是一种方法
可以以不同的方式实现它。您只需创建一些文件,例如导出数据库对象的database.js
,然后将其导入屏幕中。为了避免重复,您还可以创建一些文件,例如with database
,然后将屏幕包装到此组件中
withDatabase(AlarmDetails)
您也可以出于同样的目的使用React上下文。您应该看看上下文
我能够通过使用React上下文Api使其正常工作。您可以在此处查看示例。您可以使用适用于您的用例的确切代码。这里的数据库是什么?它是一个西瓜数据库变量@AshWinMothillas初始参数与您传递的参数浅层合并,您需要在该位置使用JSON对象,或者您需要使用HOC或Conte我喜欢下面的答案。