There are some non serializable in my React Native 0.70 app which is passed in Context API to make them available to following components. Here is code for it:
在我的Reaction Native 0.70应用程序中有一些不可序列化的东西,它在上下文API中传递,以使它们可用于以下组件。以下是它的代码:
const propsContext = React.createContext(null);
const propsVal = {
//key:value. value is non serializable
key = JSON.stringify(value), //stringify for non serializable
};
...
<propsContext.Provider value={propsVal}>
...
</propsContext.Provider>
In component which may need retrieve a non serializable value:
在可能需要检索不可序列化值的组件中:
const propsVal = useContext(propsContext);
const _value = JSON.parse(propsVal.key) // here it throw error unexpected token u
However it throws error in component:
但是,它在组件中引发错误:
SyntaxError: JSON Parse error: Unexpected token: u
Any suggestion about how to handle this non serializable value on React context?
对于如何在Reaction上下文中处理这个不可序列化的值,有什么建议吗?
更多回答
Can you try initialize your context const propsContext = React.createContext({key:””});
or key = JSON.stringify({}),
您是否可以尝试初始化上下文Const prosContext=React.createContext({key:“”});或key=JSON.stringify({}),
优秀答案推荐
我是一名优秀的程序员,十分优秀!