简体   繁体   中英

How to access the member of a different class form another class in react.js

I have a file called A.js. And in this file, I have a method doing this:

getSummaryRows() {
    const planRows = Plans.summray(this.state.selectedPlanSize, !this.state.selectedDB);

Now, I want to use the above defined planRows into another file called B.js.

I tried using it, but couldn't. Can someone tell me what's the correct way?

Where is B in the component tree, relative to A?

  • If B is a child of A, pass it as a prop.
  • If B is the parent or ancestor of A, you should probably move this code up to B and pass it down as a prop to A.
    • An alternative depending on the circumstances is to have B pass a function as a prop to A, and then have A call that function passing in the data.
  • If B is a sibling or cousin of A, move this code up to their common ancestor, and pass it as a prop to both.
  • In all cases, if these components are far apart, consider using context instead of props so you don't need to pass props through all the intermediate components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM