简体   繁体   中英

How to share data between react classes?

How can I share the data value that I have from the handleClick function inside the Module class to the InputSelect class, all the classes are in the same file?? I'm not using Redux. I can't use props because there is not a relationship between these classes.. Should I nest all the classes??

Any suggestion??

import React, { useState } from "react";

const array = [ ];
class InputSelect extends React.Component {

  render() {
      return (
       <div> 
        { 'Put it here........' }
        </div>
       )
    }
}

class Module extends React.Component {

  handleClick(e) {
    console.log(e.currentTarget.textContent);
  }

  render() {
    return (
      <div
        onClick={this.handleClick}
      >
        {this.props.id}
      </div>
    );
  }
}

function App() {
  return (
      <div>
        <Menu availableModules={array} />
      </div>
  );
}

export default App;

Correctly stated by @Federkun above, you should use React context. Check the react docs here

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