将函数传递给React中的子组件

时间:2017-05-24 21:14:12

标签: reactjs redux

我正在尝试将函数传递给React中的子组件。如果我将该功能放在ES6课程中,我可以访问this.props.dispatch,但无法访问mapStateToProps

相反,当我在ES6类之外定义函数时,我似乎可以访问函数而不是this.props.dispatch

有人可以识别我做错了什么,既可以访问this.props.dispatch,又可以在mapStateToProps函数中使用该功能

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

以下是呈现<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>
    );
  }
}

导出默认计数;

1 个答案:

答案 0 :(得分:4)

您希望将引用传递给CountableItems实例的onDenomChange属性。您可以通过引用this.onDenomChange之类的onDenomChange={this.onDenomChange}来完成此操作。

重要说明:您需要使用箭头函数() => {},以便this引用嵌套循环中的CountableItems实例。像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 {
  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);