React ES6 | Child Component not being rendered

我怕爱的太早我们不能终老 提交于 2019-12-23 05:14:01

问题


I am trying to render an array from firebase using React. I have a Container view which gets the data from firebase, and passes the data to the child component. Here is the code:

Container.jsx

import React from 'react'
import Firebase from 'firebase'
import loadsTable from './loadstable'

class Container extends React.Component{
    constructor(props){
        super(props)
        this.loads = [];
        this.state = {loads:[]}
    }
    render(){
        console.log("render" , this.state.loads)
        return( <div>
                    <loadsTable loads={this.state.loads}/>
                </div>
        )
    }
    componentWillMount(){
        this.firebaseRef = new Firebase("https://***.firebaseio.com/loads");
        this.firebaseRef.on("child_added", function(dataSnapshot) {
            console.log(dataSnapshot.val())
            this.loads.push(dataSnapshot.val());
            this.setState({
              loads: this.loads
            });
        }.bind(this));
    }
}

React.render(<Container/> , document.getElementById('app'))

this successfully gets the data (loads), and console.logs the same.

Here is the code for the child component:

LoadsTable.jsx

import React from 'react'

class loadsTable extends React.Component{

    constructor(props){
        super(props)
        console.log("init loadsTable");
        this.state = {loads:this.props.loads}
    }

    render(){
        console.log("this.state.loads " , this.props.loads);
        console.log("this.state.loads " , this.state.loads);
        var loads = this.props.loads.map(function(load, index){
            console.log("load " , load)
            return <tr><td>{load.load_number}</td></tr>
        });
        return(
            <div className="col-md-7">
                <table className="table table-hover table-bordered table-striped loads">
                    <tbody>
                        {loads}
                    </tbody>
                </table>
            </div>
        )
    }
}

export default loadsTable

The problem here is that nothing gets rendered and in fact, the console.log in the constructor is never called. If I console.log the LoadsTable in the Container constuctor, I get:

Chrome Dev Console output

function loadsTable(props) {
    _classCallCheck(this, loadsTable);

    _get(Object.getPrototypeOf(loadsTable.prototype), "constructor", this).call(this, props);
    console.log("init loadsTable");
    this.state = { loads: this.props.loads };
}

So, my question is: does anybody know what I am doing wrong here and why the LoadsTable is not being instantiated?

I am using Gulp to build and babelify to transpile the ES6.

Any help on this would be hugely appreciated. Thanks in advance.


回答1:


Component names must be capitalized. So it should be

import LoadsTable from ...;

and

<LoadsTable ... /> 

instead.

See https://facebook.github.io/react/docs/jsx-in-depth.html#html-tags-vs.-react-components



来源:https://stackoverflow.com/questions/29668632/react-es6-child-component-not-being-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!