Skip to content

getDefaultProps

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

es5风格createClass中使用getDefaultProps方法

<!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/evalJSX.js'></script>
    <script src='./dist/anu.js'></script>
    <script>
        var Parent = anu.createClass({
            displayName: 'Parent',
            getDefaultProps() {
                return {
                    name: 'world'
                }
            },
            render() {
                return evalJSX('<HelloComponent {...{name:this.props.name}} />', {
                    HelloComponent: HelloComponent,
                    this: this
                })
            }
        })

        function HelloComponent(props, context) {
            return evalJSX(`<div>Hello {props.name}</div>`, {
                this: this,
                props: props
            })
        }

        window.onload = function() {
            anu.render(Parent, document.body)
        }
    </script>
</head>

<body>

    <div>这个默认会被清掉</div>
</body>

</html>

es6风格使用getDefaultProps方法

 class Parent extends anu.Component {
            getDefaultProps() {
                return {
                    name: 'world'
                }
            }
            render() {
                return evalJSX('<HelloComponent {...{name:this.props.name}} />', {
                    HelloComponent: HelloComponent,
                    this: this
                })
            }
        }

        function HelloComponent(props, context) {
            return evalJSX(`<div>Hello {props.name}</div>`, {
                this: this,
                props: props
            })
        }

        window.onload = function() {
            anu.render(Parent, document.body)
        }

es6风格的类加es5方式的类属性

class Rect extends anu.Component {

            render() {
                return evalJSX('<Inner {...this.props} />', {
                    Inner: Inner,
                    this: this
                })
            }
        }
        Rect.defaultProps = {
            width: 100,
            height: 120,
            x: 0,
            y: 0
        }

        function Inner(props, context) {
            return evalJSX(`<div>width {props.width} * height {props.height}</div>`, {
                this: this,
                props: props
            })
        }

        window.onload = function() {
            anu.render(Rect, document.body)
        }

es6 class+ Static getter

  class Rect extends anu.Component {
            static get defaultProps() {
                return {
                    width: 100,
                    height: 120,
                    x: 0,
                    y: 0
                }
            }
            render() {
                return evalJSX('<Inner {...this.props} />', {
                    Inner: Inner,
                    this: this
                })
            }
        }


        function Inner(props, context) {
            return evalJSX(`<div>width {props.width} * height {props.height}</div>`, {
                this: this,
                props: props
            })
        }

        window.onload = function() {
            anu.render(Rect, document.body)
        }

https://github.com/facebook/react-native/issues/1772#issuecomment-237644907