简体   繁体   English

反应不渲染组件

[英]React not rendering component

This is my code: 这是我的代码:

var Item = React.createClass({
    render: function () {
        return (
                React.createElement('div', {},
                        React.createElement('span', {}, this.props.a),
                        React.createElement('span', {}, this.props.b)
                        )
                );
    }
});

var RParent = React.createClass({
    getInitialState: function () {
        return {messages: []};
    },
    addMess: function (data) {
        var self = this;
        self.state.messages.push({
            a: data.a,
            b: data.b
        });
        this.setState({messages: self.state.messages});
    },
    render: function () {
        var messages = this.state.messages;
        return (
                React.createElement('div', {},
                        React.createElement(Item, messages))
                );
    }
});

var box = ReactDOM.render(React.createElement(RParent), document.getElementById('parentDiv'));

function render(a, b) {
    box.addMess({
        a: a,
        b: b
    });
}

Data is sent to the render function. 数据发送到render功能。 It appears that somehow the properties are not reaching the render function of the Item as they appear to be undefined when I try print them to console. 似乎属性以某种方式未达到Item的渲染功能,因为当我尝试将其打印到控制台时,它们似乎是undefined

Any ideas why this might be happening? 任何想法为什么会发生这种情况?

The problem is that nothing is rendered into the "parentDiv". 问题在于“ parentDiv”中没有呈现任何内容。 It just remains blank. 它只是空白。 However, on inspecting the elements, I can still see two "span" children under it (which were not created in html or anywhere else except here). 但是,在检查元素时,我仍然可以看到其下的两个“ span”子元素(不是在html或此处以外的其他任何地方创建的)。 But those span elements have no content. 但是那些span元素没有内容。

I think there's a fundamental problem to your code. 我认为您的代码存在一个基本问题。 You are trying to pass 'a' and 'b' to your react component through an external function 'render' that seems to be invoking one of the component's functions. 您试图通过似乎正在调用组件功能之一的外部函数“ render”将“ a”和“ b”传递给您的react组件。 This is wrong. 错了

You should pass 'a' and 'b' to your React component RParent as props. 您应该将“ a”和“ b”作为道具传递给React组件RParent。 Whenever the value of 'a' and 'b' change, your parent component will automatically rerender. 每当“ a”和“ b”的值更改时,父组件将自动重新呈现。

var Item = React.createClass({
render: function () {
    return (
            React.createElement('div', {},
                    React.createElement('span', {}, this.props.a),
                    React.createElement('span', {}, this.props.b)
                    )
            );
}
});

var RParent = React.createClass({
getInitialState: function () {
    return {messages: []};
},
,
render: function () {
    var messages = this.props.messages;
    return (
            React.createElement('div', {},
                    React.createElement(Item, messages))
            );
}
});

ReactDOM.render(
    (<RParent messages= {'a': a, 'b': b} />)
    , document.getElementById('parentDiv'));

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

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