简体   繁体   English

如何在反应类之间共享数据?

[英]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??如何将模块 class 内的 handleClick function 中的数据值共享到 InputSelect class,所有类都在同一个文件中? I'm not using Redux.没有使用 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.上面@Federkun 正确地说明了,你应该使用 React 上下文。 Check the react docs here此处查看反应文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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