Javascript 如何在React中将数据从子HOC组件传递到其父组件。我使用的是材料界面';s可选列表,它是一个特殊组件

Javascript 如何在React中将数据从子HOC组件传递到其父组件。我使用的是材料界面';s可选列表,它是一个特殊组件,javascript,reactjs,higher-order-components,Javascript,Reactjs,Higher Order Components,父组件, 我通常会将一个函数作为道具传递给子组件 import React, { Component } from 'react'; import ListExampleSelectable from './List'; export default class App extends Component { constructor(props) { super(props) this.state = { childValue: '', };

父组件, 我通常会将一个函数作为道具传递给子组件

import React, { Component } from 'react';
import ListExampleSelectable from './List';


export default class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      childValue: '',
    };
    this.handleClick = this.handleClick.bind(this)
  }

  handleClick(event) {
    this.setState({
      childValue: event
    })
  }


  render() {
    return (
      <div>
        // I would usually just pass in a function here to handle the data 
        <ListExampleSelectable someFunction={this.handleClick} />
      </div>
    );
  }
}
import React,{Component}来自'React';
导入列表示例可从“/List”中选择;
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具)
此.state={
childValue:“”,
};
this.handleClick=this.handleClick.bind(this)
}
handleClick(事件){
这是我的国家({
childValue:事件
})
}
render(){
返回(
//我通常只在这里传入一个函数来处理数据
);
}
}
子组件, 在这里,我通常只调用传递的函数并返回数据,但由于它是一个HOC组件,我得到一个错误,说“\u this.props.someFunction不是函数”,但我不确定为什么

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {List, ListItem, makeSelectable} from 'material-ui/List';
import Avatar from 'material-ui/Avatar';
import Subheader from 'material-ui/Subheader';

let SelectableList = makeSelectable(List);

function wrapState(ComposedComponent) {
  return class SelectableList extends Component {
    static propTypes = {
      children: PropTypes.node.isRequired,
      defaultValue: PropTypes.number.isRequired,
      someFunction: PropTypes.func,
    };

    // I added the constructor in here because I was not sure why it was removed thinking that was why the component was not receiving the props???
    constructor(props) {
      super(props)
    }

    componentWillMount() {
      this.setState({
        selectedIndex: this.props.defaultValue,
      });
    }

    handleRequestChange = (event, index) => {
      this.setState({
        selectedIndex: index,
      });
      // this works when the child component is just a normal component and not an HOC component????
      this.props.someFunction(event);
    };

    render() {
      return (
        <ComposedComponent
          value={this.state.selectedIndex}
          onChange={this.handleRequestChange}
        >
          {this.props.children}
        </ComposedComponent>
      );
    }
  };
}

SelectableList = wrapState(SelectableList);

const ListExampleSelectable = () => (

    <SelectableList defaultValue={3}>
      <Subheader>Selectable Contacts</Subheader>
      <ListItem
        value={1}
        primaryText="Brendan Lim"
        leftAvatar={<Avatar src="images/ok-128.jpg" />}
        nestedItems={[
          <ListItem
            value={2}
            primaryText="Grace Ng"
            leftAvatar={<Avatar src="images/uxceo-128.jpg" />}
          />,
        ]}
      />
      <ListItem
        value={3}
        primaryText="Kerem Suer"
        leftAvatar={<Avatar src="images/kerem-128.jpg" />}
      />
      <ListItem
        value={4}
        primaryText="Eric Hoffman"
        leftAvatar={<Avatar src="images/kolage-128.jpg" />}
      />
      <ListItem
        value={5}
        primaryText="Raquel Parrado"
        leftAvatar={<Avatar src="images/raquelromanp-128.jpg" />}
      />
    </SelectableList>

);

export default ListExampleSelectable;
import React,{Component}来自'React';
从“道具类型”导入道具类型;
从“物料ui/List”导入{List,ListItem,makeSelectable};
从“材质ui/化身”导入化身;
从“物料界面/分标题”导入分标题;
让SelectableList=makeSelectable(列表);
函数包装状态(ComposedComponent){
返回类SelectableList扩展组件{
静态类型={
子项:PropTypes.node.isRequired,
defaultValue:PropTypes.number.isRequired,
someFunction:PropTypes.func,
};
//我在这里添加了构造器,因为我不知道为什么它会被删除,我想这就是为什么组件没有收到道具???
建造师(道具){
超级(道具)
}
组件willmount(){
这是我的国家({
selectedIndex:this.props.defaultValue,
});
}
HandlerRequestChange=(事件、索引)=>{
这是我的国家({
selectedIndex:index,
});
//当子组件只是普通组件而不是HOC组件时,此功能有效????
this.props.someFunction(事件);
};
render(){
返回(
{this.props.children}
);
}
};
}
SelectableList=包装状态(SelectableList);
常量ListExampleSelectable=()=>(
可选触点
);
导出默认列表示例可选;

你能解释一下你的目标是什么吗?也许我可以建议你进行代码重构,其中可能包括你的解决方案。是的,我只是重构了它,因为我无法找到一种方法将状态从子组件传递到父组件。父级应该向子HOC组件传递一个列表,然后子HOC组件应该告诉父级单击了哪些列表项。我只是使用了一个非特殊组件来完成这项工作,这使得传递状态变得很容易。