简体   繁体   English

如何从其他功能访问道具中的功能

[英]How to access a function in props from another function

I am making first steps with React, so sorry if my question is basic/stupid. 我正在使用React迈出第一步,如果我的问题是基本/愚蠢的,那么抱歉。 :) :)

I have connected an Action from a Class in order to have access to that from this.props.selectUser(user) : 我已经连接了一个Class的Action,以便可以从this.props.selectUser(user)

function matchDispatchToProps(dispatch) {
    return bindActionCreators({
        selectUser: selectUser
    }, dispatch);
}

export default connect(mapStateToProps, {matchDispatchToProps })(UserList);

Then I used a component to make DataTables (react-bootstrap-table) and I set selectRow={selectRowProp} to add interaction and call a class function: 然后,我使用一个组件来制作DataTables(react-bootstrap-table),并设置selectRow={selectRowProp}以添加交互并调用类函数:

<BootstrapTable data={mappedUsers} selectRow={selectRowProp} >
    <TableHeaderColumn isKey dataField='id'>ID</TableHeaderColumn>
    ...
</BootstrapTable>

this is the option that state to call onRowSelect on row selecting : 这是状态选择行时调用onRowSelect的选项:

const selectRowProp = {
    mode: 'checkbox',
    clickToSelect: true,
    hideSelectColumn: true,
    onSelect: onRowSelect
}

Within the function is not possible to access this.props.selectUser(row.user); 在该函数内无法访问this.props.selectUser(row.user);

function onRowSelect( row, isSelected, e) {
    console.log("THIS: ", this);
    this.props.selectUser(row.user);
}

How can I do? 我能怎么做? Please help! 请帮忙! This is the whole cleaned code: 这是全部清除的代码:

import React, {Component, PropTypes} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {selectUser} from '../actions/index';
import ReactBsTable,{BootstrapTable, TableHeaderColumn} from 'react-bootstrap-table';
import {Link} from 'react-router';

class UserList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selected: [],
            currPage: 1
        };
    }

    render() {
        const {  currPage } = this.state;
        populateUserList(this.props.users.content);

        function onRowSelect( row, isSelected, e) {
            this.props.selectUser(row.user);
        }

        const options = {
            sizePerPageList: [ 5, 10, 15, 20 ],
            sizePerPage: 10,
            page: currPage,
            sortName: 'id',
            sortOrder: 'desc'
        };

        const selectRowProp = {
            mode: 'checkbox',
            clickToSelect: true,
            hideSelectColumn: true,
            onSelect: onRowSelect
        }

        return (
            <div>
                <BootstrapTable data={mappedUsers} striped  selectRow={selectRowProp} pagination={true} options={options}  >
                    <TableHeaderColumn isKey dataField='id'>ID</TableHeaderColumn>
                    <TableHeaderColumn dataField='name'>Name</TableHeaderColumn>                   
                </BootstrapTable>

                <Link to="/" className="btn btn-info btn-lg pull-right">Back to Search</Link>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        mappedUsers: state.mappedUsers
    };
}

function matchDispatchToProps(dispatch) {
    return bindActionCreators({
        selectUser: selectUser
    }, dispatch);
}

export default connect(mapStateToProps, {matchDispatchToProps })(UserList);

this in your function refers to the function but you actually want this to refer to the class. 该函数中的this引用该函数,但实际上您希望此对象引用该类。

You could use 'const self = this' inside the render. 您可以在渲染器中使用“ const self = this”。 (and use self.selectUser(row.user)) (并使用self.selectUser(row.user))

Or make onRowSelect a method of UserList (outside of the render method) and add in the constructor this.onRowSelect = this.onRowSelect.bind(this) so this will refer to your class and therefore you could access your props. 或使onRowSelect为UserList的方法(在render方法之外)并在构造函数中添加this.onRowSelect = this.onRowSelect.bind(this),这样它将引用您的类,因此您可以访问道具。

the render method is called often in React so, I think you should avoid declaring functions and too much stuff in it. render方法在React中经常被调用,因此,我认为您应该避免声明函数和太多的东西。

Or better could refer to this article : http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/ 或者更好的可以参考这篇文章: http : //reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/

No need to map the mapDispatchToProps parameter of redux's connect() into another object, bindActionCreators() already does that for you : 无需将redux的connect()mapDispatchToProps参数映射到另一个对象中, bindActionCreators()已经为您完成了这一工作

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        selectUser: selectUser
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(UserList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM