How do I pass data from a child HOC component to its parent in React. I am using Material UI's Selectable List which is an HOC component

Viewed 1031

Parent Component, I would normally pass a function as a prop to the child component

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

child component, Inside here I would just normally call the function that is passed and return the data but since its an HOC component I get an error saying "_this.props.someFunction is not a function" but I am not sure why?

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;
0 Answers
Related