函数中的 ReactJS onClick 返回未检测到 JSX

ReactJS onClick in function returned JSX not detected

在我的渲染中,我有一个函数 returns 一个 JSX 块:

function userBlock(category) {
    return (
        <div className="row">
            <div className="col-md-12 pb15">
                <Chip icon={ category } name={ category }/>
            </div>
            <div className="col-md-12 pb15">
                <div className="well">
                    <p className="lead text-center">You have not yet created any lists.</p>
                    <p className="text-center">
                        <a onClick={ createNewList }><i className="fa fa-plus-circle"></i> New List</a>
                    </p>
                </div>
            </div>
        </div>
    )
}

目前 createNewList 函数没有触发,起初我尝试使用 this.createNewList 并将其绑定到构造函数。然而,这引发了一个错误...

TypeError: Cannot read property 'createNewList' of undefined

constructor(props) {
    super(props);
    this.state = {
        category: props.category,
        category_cards: []
    };
    this.createNewList = this.createNewList.bind(this);
}

createNewList() {
    console.log('1 createNewList');
}

render() {

    function createNewList() {
        console.log('2 createNewList');
    }

完整代码


import React from 'react';
import Chip from '../Chip';

export class List extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            category: props.category,
            category_cards: []
        };
        this.createNewList = this.createNewList.bind(this);
    }

    createNewList() {
        console.log('1 createNewList');
    }

    render() {

        function createNewList() {
            console.log('2 createNewList');
        }

        function defaultBlock(category) {
            // return <Categories category={ category }/>
            return <h1>Categories component goes here</h1>
        }

        function userBlock(category) {
            return (
                <div className="row">
                    <div className="col-md-12 pb15">
                        <Chip icon={ category } name={ category }/>
                    </div>
                    <div className="col-md-12 pb15">
                        <div className="well">
                            <p className="lead text-center">You have not yet created any lists.</p>
                            <p className="text-center">
                                <a onClick={ createNewList }><i className="fa fa-plus-circle"></i> New List</a>
                            </p>
                        </div>
                    </div>
                </div>
            )
        }

        function toggler(category) {
            return category === 'user' ? userBlock(category) : defaultBlock(category);
        }

        return (
            <div className="container">
                { toggler(this.state.category) }
            </div>
        );
    }
}

export default List;

这是一个范围问题,在 class 中定义所有 function(在 render 方法之外)并通过 this.functionName() 调用它们。

使用这个,检查工作代码:

class List extends React.Component {
    
        constructor(props) {
            super(props);
            this.state = {
                category: props.category,
                category_cards: []
            };
            this.createNewList = this.createNewList.bind(this);
        }
    
        createNewList() {
            console.log('2 createNewList');
        }
    
        defaultBlock(category) {
            return <h1>Categories component goes here</h1>
        }
    
        userBlock(category) {
            return (
                <div className="row">
                    <div className="col-md-12 pb15">
                        <Chip icon={ category } name={ category }/>
                    </div>
                    <div className="col-md-12 pb15">
                        <div className="well">
                            <p className="lead text-center">You have not yet created any lists.</p>
                            <p className="text-center">
                                <a onClick={ this.createNewList }><i className="fa fa-plus-circle"></i> New List</a>
                            </p>
                        </div>
                    </div>
                </div>
            )
        }
    
         toggler(category) {
            return category === 'user' ? this.userBlock(category) : this.defaultBlock(category);
        }
    
        render() {        
    
            return (
                <div className="container">
                    { this.toggler(this.state.category) }
                </div>
            );
        }
    }

ReactDOM.render(<List/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>