Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

React - accessing context in child components

I am using this react package - @spyna/react-store (link) in my project which is nothing but a wrapper for React Context API - createStore & withStore. But I am not able to access the react context api store values in my child component (which I set in the App component).

Actually when I try to access this.props.store.get("amount") I am getting an compilation error saying "props store does not exist". Please advise me on how to access the context in my child component. Thanks in advance.

App component:

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);

Child component:

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)
question from:https://stackoverflow.com/questions/65874831/react-accessing-context-in-child-components

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...