Skip to content

context的使用

司徒正美 edited this page Mar 7, 2017 · 4 revisions

context只传递一层

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <script src='./dist/jsx-parser.js'></script>
    <script src='./dist/genCode.js'></script>
    <script src='./dist/anu.js'></script>
    <script>
    

        var Parent = anu.createClass({
            childContextTypes: {
                value: anu.PropTypes.string
            },

            render: function() {
                return evalJSX(`
    	<div id="333">
        {this.context.value}
      </div>`, {
                    this: this
                });
            }
        });

        var App = anu.createClass({
            getChildContext: function() {
                return {
                    value: '6666'
                };
            },
            render: function() {
                return evalJSX(`
      <Parent/>`, {
                    this: this,
                    Parent: Parent
                });
            }
        });

        window.onload = function() {
            var result = anu.render(anu.createElement(App), document.getElementById('container'))
            console.log(result)
        }
    </script>
</head>

<body>
    <div id="container">
        <!-- This element's contents will be replaced with your component. -->
    </div>
    <div>这个默认会被清掉</div>
</body>

</html>