• 父子组件之间传递数据


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="http://cdn.bootcss.com/react/0.14.1/react.js"></script>
        <script src="http://cdn.bootcss.com/react/0.14.1/react-dom.js"></script>
        <script src="http://cdn.bootcss.com/babel-core/5.8.32/browser.min.js"></script>
        </head>
    <body>
        <div id="App">
            
        </div>
    </body>
    <script type="text/babel">
        
        class ParentComp extends React.Component{
            constructor(...args){
                super(...args);
                this.state = {
                    value:1
                }
            }
    
            fn(){
                this.setState({
                    value: this.state.value + 1
                })            
            }
            render(){
                return (
                    <div>
                <input type = "button" value = "aaa" 
                onClick = {this.fn.bind(this)} />
                <ChildComp name = {this.state.value} />//为ChildComp组件指定了一个name属性
                </div>
                )    
            }
        }
    
        class ChildComp extends React.Component{
            
            render(){
                return <span>{this.props.name}</span>//在组件类中使用props获取属性
            }
        }
    
        window.onload = function(){
            var div = document.getElementById('App');
            ReactDOM.render(
            <ParentComp/>,
            div
            );
        }
        
    </script>
    </html>

    在父组件的输出中可以引用子组件。 属性是从父组件传递给子组件的。

    组件的属性:

      指定属性:一般是在< />这种标签中指定属性,这时候属于

      获取属性:在组件类中使用this.props获取属性

    上例中,我们在父组件中设置 state, 并通过在子组件上使用 props 将其传递到子组件上。而最后的渲染只需要渲染父组件即可。  

  • 相关阅读:
    STM32的备份寄存器测试
    dsp6657的helloworld例程测试-第一篇
    Dennis Gabor与全息摄影
    Gabor filter与Gabor transform
    图像生成器:让电脑学习生成数字图像
    Haar-like feature和Haar wavelet
    《Wonderland: A Novel Abstraction-Based Out-Of-Core Graph Processing System》章明星
    小波分析及其应用
    Discrete cosine transform(离散余弦转换)
    psimpl_v7_win32_demo
  • 原文地址:https://www.cnblogs.com/Jerry-spo/p/6664151.html
Copyright © 2020-2023  润新知