ReactJS组件函数调用此函数,props未定义

Ben

这可能是一个简单的React问题,但是我是一个新手。在完成了一些教程和出色的在线课程之后,我正在尝试我的第一个适当的ReactJS项目。我也咬过ES6和JSX,所以可能是问题所在。基本上,使用传入的数组作为props,然后调用子函数对数据结构进行登录,我想将被称为onClick的函数作为props传入,但是“ this”在函数中未定义,尽管它是被传递给array.map函数...

import React from 'react';
import { Link, Location } from 'react-router';

import moment from 'moment';
import _ from 'lodash';

import IBoxTools from '../common/iboxTools';

class Timesheets extends React.Component {

    componentDidMount() {
        $('.timesheets li button').tooltip();
    }

    render() {

        const showTimesheets = function (timesheets) {

            if (timesheets && timesheets.length) {
                return (
                    <div>
                        <ul className="list-group timesheets">
                            {timesheets.map(timesheetCandidateRow, this)}
                        </ul>
                        <Link to="/timesheets" className="btn btn-primary">View all timesheets</Link>
                    </div>
                );
            }
            else {
                return (
                    <div>
                        <p>There are currently no timesheets requiring action</p>
                        <a className="btn btn-primary" href="#">View all timesheets</a>
                    </div>);
            }
        }

        const timesheetCandidateRow = function (timesheetCandidate) {
            var candidateName = timesheetCandidate.candidate.forename + ' ' + timesheetCandidate.candidate.surname;
            return (
                <li className="list-group-item" key={timesheetCandidate.id}>
                    <div>
                        <h4 className="list-group-item-heading">{candidateName}<br /><small>{timesheetCandidate.candidate.title}</small></h4>
                        <table className="table table-condensed">
                            <tbody>
                                {timesheetCandidate.timesheets.map(timesheetRow, this)}
                            </tbody>
                        </table>
                    </div>
                </li>
            );
        }

        const timesheetRow = function (timesheet) {
            var timesheetDate = moment(timesheet.date).format('dddd Do MMM');
            if (timesheet.startTime) {
                timesheetDate = timesheetDate + ' ' + timesheet.startTime + ' to ' + timesheet.endTime;
            }
            return (
                <tr key={timesheet.id}>
                    <td>{timesheetDate}</td>
                    <td>{timesheet.charge}</td>
                    <td style={{ width: 70 }}>
                        <div className="btn-group">
                            <button className="btn btn-success btn-xs" onClick={this.props.onAccept} title="Accept" data-toggle="tooltip" data-placement="bottom"><i className="fa fa-check"></i></button>
                            <button className="btn btn-danger btn-xs" onClick={this.props.onReject} title="Reject" data-toggle="tooltip" data-placement="bottom"><i className="fa fa-close"></i></button>
                        </div>
                    </td>
                </tr>
            );
        }

        return (
            <div className="ibox float-e-margins">
                <div className="ibox-title">
                    <h5>
                        <span className="fa-stack">
                            <i className="fa fa-square-o fa-stack-2x"></i>
                            <i className="fa fa-check fa-stack-1x"></i>
                        </span> Timesheets
                                </h5>
                    <IBoxTools />
                </div>
                <div className="ibox-content">
                    {showTimesheets(this.props.timesheets)}
                </div>
            </div>
        );
    }

}

Timesheets.propTypes = {
    timesheets: React.PropTypes.array,
    onAccept: React.PropTypes.func.isRequired,
    onReject: React.PropTypes.func.isRequired
}

export default Timesheets;

因此,在render方法的const中,“ this”是未定义的。有什么帮助吗?

谢谢。

胡安·门德斯

要“锁定” this,可以使用箭头功能

 const showTimesheets =  (timesheets) => {...}

同样为timesheetCandidateRow与任何其他内部函数。

您也可以使用Vlad发布的绑定解决方案,但这要求您确保调用的函数传递正确的信息this(使用bind,call或apply)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

在地图函数Reactjs中未定义“ this”

来自分类Dev

未定义对函数调用的引用?

来自分类Dev

PHP错误:调用未定义函数

来自分类Dev

调用未定义的函数,为什么?

来自分类Dev

调用未定义的函数__()错误-phpMyAdmin

来自分类Dev

jQuery-函数未定义-从内联脚本调用此函数

来自分类Dev

PHP调用未定义函数newuserid()

来自分类Dev

调用未定义函数:Laravel 5.1

来自分类Dev

尚未定义的C ++调用函数

来自分类Dev

如果未定义,则跳过函数调用

来自分类Dev

调用未定义的函数execute()

来自分类Dev

将函数传递给子组件this.props.children方法未定义

来自分类Dev

尝试调用未定义的函数glutInit

来自分类Dev

其他函数内部的reactjs es6调用函数抛出未定义的函数

来自分类Dev

未定义的函数调用

来自分类Dev

heroku对未定义函数imagecreatefromjpeg()的调用

来自分类Dev

调用未定义的函数?

来自分类Dev

调用未定义的函数logging_in()

来自分类Dev

调用未定义的函数cell()

来自分类Dev

调用函数上的未定义变量

来自分类Dev

调用未定义的函数

来自分类Dev

调用未定义的函数ecrypt()

来自分类Dev

自调用函数返回“未定义”

来自分类Dev

将函数传递给子组件this.props.children方法未定义

来自分类Dev

调用未定义的函数execute()

来自分类Dev

致命错误:调用未定义的函数

来自分类Dev

phpMyAdmin错误调用未定义的函数

来自分类Dev

es6类组件中我的函数内部未定义props

来自分类Dev

调用未定义的函数mysqli()