Pass function down to child Component in React

All we need is an easy explanation of the problem, so here it is.

I am trying to pass a function down to a child component in React. If I place the function within the ES6 class I have access to this.props.dispatch, but do not have access within mapStateToProps

Conversely when I define the function outside of the ES6 class I seem to have access to the function but not this.props.dispatch

Can someone identify what I am doing wrong to have both access to this.props.dispatch as well as having the function available in mapStateToProps function

import React, { Component } from 'react';
import { connect } from 'react-redux';
import DenomInput from '../denomInput/DenomInput.js';
import * as actions from '../../actions/countActions';

class CountableItems extends Component {
  constructor(props) {
    super(props);
    this.onDenomChange = this.onDenomChange.bind(this);
  }

  onDenomChange = (value, denom) => {
    this.props.dispatch(actions.increaseSum(value, denom));
    console.log(value);
  }

  render() {
    const props = this.props;

    let denomGroups = props.denoms.map(function (denom, i) {
      return (
        Object.keys(denom).map(function (key) {
          let denoms = denom[key].map(function (item, i) {
            return <DenomInput denom={item} onDenomChange={props.onDenomChange} key={i}></DenomInput>
          });
          return (<div className="col"><h2>{key}</h2>{denoms}</div>)
        })
      );
    });

    return (
      <div className="countable-item-wrapper">
        <div className="row">
          {denomGroups}
        </div>
      </div>
    );
  }
}

function mapStateToProps(state, ownProps) {
  return {
    denoms: state.count.denomGroups,
    onDenomChange: this.onDenomChange
  }
}

export default connect(mapStateToProps)(CountableItems);

Here is the component that renders <CountableItems>

import React, { Component } from 'react';
import CountableItems from '../components/countableItems/CountableItems.js';
import CountSummary from '../components/countSummary/CountSummary.js';

import "./Count.css";

class Count extends Component {
  render() {
    return (
      <div className="container">
        <div className="row">
          <div className="col-8">
            <CountableItems />
          </div>
          <div className="col-4">
            <CountSummary />
          </div>
        </div>
      </div>
    );
  }
}

export default Count;

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You want to pass a reference to the CountableItems instance’s onDenomChange property. You can do that by referencing this.onDenomChange like onDenomChange={this.onDenomChange}.

Important note: you need to use arrow functions () => {} in order for for this to reference the CountableItems instance inside your nested loops. Regular functions like function () {} would not use the correct context.

import React, { Component } from 'react';
import { connect } from 'react-redux';
import DenomInput from '../denomInput/DenomInput.js';
import * as actions from '../../actions/countActions';

class CountableItems extends Component {
  onDenomChange = (value, denom) => {
    this.props.dispatch(actions.increaseSum(value, denom));
    console.log(value);
  }

  render() {
    let denomGroups = this.props.denoms.map((denom, i) => {
      return (
        Object.keys(denom).map((key) => {
          let denoms = denom[key].map((item, i) => {
            return <DenomInput denom={item} onDenomChange={this.onDenomChange} key={i}></DenomInput>
          });
          return (<div className="col"><h2>{key}</h2>{denoms}</div>)
        })
      );
    });

    return (
      <div className="countable-item-wrapper">
        <div className="row">
          {denomGroups}
        </div>
      </div>
    );
  }
}

function mapStateToProps(state, ownProps) {
  return {
    denoms: state.count.denomGroups,
  }
}

export default connect(mapStateToProps)(CountableItems);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply