this.props.children 踩坑

问题:

import React from 'react';
import ReactDOM from 'react-dom';
// import {BrowerRouter as Router,Switch,Route,Link} from '';

// import Home from 'page/home/index.jsx';
class A extends React.Component {
	constructor (props){
		super	(props)

	}
	render(){
		return <div>component A</div>

	}
}
class B extends React.Component {
	constructor (props){
		super	(props)
	}
	render(){
		return <div>component B</div>

	}
}
//容器
class Wrapper extends React.Component {
	constructor(props){
		super	(props)
	}

	render() {
		return (
			<div>
				{this.props.children}
		 </div>
		)

	}
}

ReactDOM.render(
	<Wrapper>
		<A/>
		<B/>
	<Wrapper/>,
	document.getElementById('app')
)

报错:

排除了一下错,原来是因为:

wrapper的闭合标签写错了。

正确的写法应该是:

import React from 'react';
import ReactDOM from 'react-dom';
// import {BrowerRouter as Router,Switch,Route,Link} from '';

// import Home from 'page/home/index.jsx';
class A extends React.Component {
    constructor (props){
        super    (props)

    }
    render(){
        return <div>component A</div>

    }
}
class B extends React.Component {
    constructor (props){
        super    (props)
    }
    render(){
        return <div>component B</div>

    }
}
//容器
class Wrapper extends React.Component {
    constructor(props){
        super    (props)
    }

    render() {
        return (
            <div>
                {this.props.children}
         </div>
        )

    }
}

ReactDOM.render(
    <Wrapper>
        <A/>
        <B/>
    </Wrapper>,
    document.getElementById('app')
)

 

posted @ 2018-09-22 15:04  haines  阅读(1628)  评论(0编辑  收藏  举报