我在我的项目中使用这个反应包 - @spyna/react-store (链接),它只是 React Context API - createStore 和 withStore 的包装器。但是我无法访问我的子组件(我在 App 组件中设置)中的反应上下文 api 存储值。
实际上,当我尝试访问 this.props.store.get("amount") 时,我收到一个编译错误,提示“道具商店不存在”。请告诉我如何访问我的子组件中的上下文。提前致谢。
应用组件:
import { createStore } from "@spyna/react-store";
class App extends React.Component<IAppProps> {
render() {
<Route component={this.ChildComponent} />
}
private ChildComponent= () => <Child1/>;
}
const initialValue = {
amount: 15,
username: {
name: "spyna",
url: "https://spyna.it"
}
};
export default createStore(App, initialValue);
子组件:
import { withStore } from "@spyna/react-store";
interface ICustomProps {
...
}
interface IAppProps {
...
}
class Child1 extends React.Component<ICustomProps & IAppProps, IAuthState> {
render() {
return (
<p>My Amount: {this.props.store.get('amount')}</p>
)
}
}
export default withStore(Child1)