简体   繁体   English

React.js-组件作为CommonJS模块-事件不起作用

[英]React.js - Component as CommonJS Module - Events not working

I'm in the very early phase of learning React.js. 我正处于学习React.js的早期阶段。 I'm trying to learn how to modularize React.js components. 我正在尝试学习如何模块化React.js组件。

I make a React.js component as a CommonJS module, and when I invoke it from other module - all seem to work except that the events doesn't seem to bind to the view when the component re-renders after a state change. 我将React.js组件制作为CommonJS模块,并且当我从其他模块调用它时,所有组件似乎都可以正常工作,只是当状态更改后组件重新渲染时,事件似乎没有绑定到视图。

I've place my code at the end. 我将代码放在最后。

I've placed the code for TestComponent in a file name component.jspx . 我已经将TestComponent的代码放置在文件名component.jspx Then I use the component from app.js 然后我使用来自app.js的组件

Per the code in TestComponent , the component on it's initialState will have two list-items (with a button that triggers an event). 根据TestComponent的代码,其initialState上的组件将具有两个列表项(带有触发事件的按钮)。

Then on componentDidMount , I change the state with an additional data. 然后在componentDidMount ,使用其他数据更改状态。

The component renders properly - except that the event handleClickEvenet doesn't seem to get bound for the updated view. 该组件可以正确呈现-除了事件handleClickEvenet似乎没有绑定到更新的视图之外。

Please help me understand how to fix this problem. 请帮助我了解如何解决此问题。

// 
// component.jsx

var TestComponent =React.createClass({

    handleClickEvent: function() {
        alert ('Clicked...');
    },

    getInitialState: function() {
        return {
            comments: [
                {author: 'A1', comment: 'Comment by A1'},
                {author: 'A2', comment: 'Comment by A2'}
            ]
        };
    },

    componentDidMount: function() {
        var comments = this.state.comments;
        comments.push({author: 'A3', comment: 'Comment by A3'});
        this.setState({comments: comments});
    },

    render: function() {
        var thisComponent = this;
        return (
                <ol>
                    {
                        this.state.comments.map (function(comment, i) {
                            return (
                                    <li>
                                        {comment.comment} | {comment.author} |
                                        <button onClick={thisComponent.handleClickEvent}>Click me</button>
                                    </li>
                                    )
                        })
                    }
                </ol>
        )
    }
})

module.exports = TestComponent;


// -------
// app.js
// -------

var CommentBox = require('./components/Demo.jsx');
var React = require('../components/react/react-with-addons.js');

var commentBox = React.createElement(CommentBox, data);
React.render(commentBox, document.body);

Tested out the stuff you supplied, since it actually looks as it should. 测试了您提供的内容,因为它实际上看起来应该正确。

Doing a few minor changes to it so it could execute in jsfiddle, it should look like this ( jsfiddle ): 对其进行一些小的更改,使其可以在jsfiddle中执行,它看起来应该像这样( jsfiddle ):

var CommentBox = React.createClass({

    handleClickEvent: function() {
        alert ('Clicked...');
    },

    getInitialState: function() {
        return {
            comments: [
                {author: 'A1', comment: 'Comment by A1'},
                {author: 'A2', comment: 'Comment by A2'}
            ]
        };
    },

    componentDidMount: function() {
        var comments = this.state.comments;
        comments.push({author: 'A3', comment: 'Comment by A3'});
        this.setState({comments: comments});
    },

    render: function() {
        var thisComponent = this;
        return (
                <ol>
                    {
                        this.state.comments.map (function(comment, i) {
                            return (
                                    <li>
                                        {comment.comment} | {comment.author} |
                                        <button onClick={thisComponent.handleClickEvent}>Click me</button>
                                    </li>
                                    )
                        })
                    }
                </ol>
        )
    }
});

var commentBox = React.createElement(CommentBox, {});
React.render(commentBox, document.body);

I guess the problem is something minor and is nothing big. 我猜问题出在小问题上,没有什么大不了的。 Test what you done using the supplied jsfiddle and consider simplifying your setup until it works and take it from there. 使用提供的jsfiddle测试您所做的事情,并考虑简化设置,直到可行为止,然后从那里开始。

Have a good bug hunt :) 进行良好的错误查找:)

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

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