Skip to content

Stateless Component

司徒正美 edited this page May 19, 2017 · 2 revisions

无状态组件就是一个返回虚拟DOM的函数,没有生态周期,数据只能通过参数传递

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
   
    <script src='./dist/React.js'></script>
    <script src="./test/babel.js"></script>
    <script type='text/babel'>
        //无状态组件
        function HelloComponent(props, context) {
            return <div className="Hello">Hello {props.name}</div>
        }

       class App extends React.Component{
            getChildContext() {
                return {
                    value: 666
                };
            }
            render() {
    	       return (<div className='app'><HelloComponent name='司徒正美' /></div>)
            }
        };


        window.onload = function() {
            ReactDOM.render(<App />, document.getElementById('example'));
        }
    </script>
</head>

<body>
    <div id='example'>这个默认会被清掉</div>
</body>

</html>