gpt4 book ai didi

JSON.stringify/parse throw error when handling non-serializable context in react native(JSON.stringify/parse在React本机中处理不可序列化上下文时出现错误)

转载 作者:bug小助手 更新时间:2023-10-25 12:16:51 27 4
gpt4 key购买 nike



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({}),

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com