我试图在React Native应用上使用React的Context API,但每当我尝试使用上下文提供程序时,我都会收到错误。我无法弄清楚错误是什么。
我有以下代码:
// PlayerContext.ts
import React from "react";
export const PlayerContext = React.createContext(222);
// PlayerContextProvider.tsx
import React from "react";
import { PlayerContext } from "./PlayerContext";
export default class PlayerContextProvider extends React.Component {
public render() {
return (
<PlayerContext.Provider value={222}>
{this.props.children}
</PlayerContext.Provider>
);
}
}
//HomePage.tsx
import * as React from "react";
import { Text } from "react-native";
import PlayerContextProvider from "./Player/PlayerContextProvider";
export default class HomePage extends React.Component {
public render() {
return (
<PlayerContextProvider>
<Text>test</Text>
</PlayerContextProvider>
);
}
}
尝试运行应用时出现此错误:
Attempted to assign to readonly property.
popProvider
ReactNativeRenderer-dev.js:12077:12
unwindWork
[...]
我做错了什么?