zoukankan      html  css  js  c++  java
  • React入门---属性(props)-8

    Props 和 State对于组件Component是非常重要的两个属性。

    区别:State对于模块来说是 自身属性;

         Props对于模块来说是 外来属性;

    同样的,props也是只作用于当前的组件,绝不影响其他组件;

    给组件 <ComponentFooter> 添加props属性和属性值;

    例:从父组件index.js给子组件footer.js传送数据:

    class Index extends React.Component{
        render(){
            return(
                //里面分别是 头部 主体 底部
                <div>
                    <ComponentHeader/>
                    <BodyIndex/>
                    {/*在这里给footer组件,添加props外来属性,不会影响其他组件*/}       
                    <ComponentFooter userId={123456}/> 
                </div>
                );
        }
    }

    然后在footer组件里面,通过props属性来接收;

    export default class ComponentFooter extends React.Component{
        render(){
            return(
                    <div>
                        <h1>这里是底部</h1>
                        {/*props接收来自index.js的信息,在页面显示*/}
                        <p>{this.props.userId}</p> 
                    </div>
                )
        }

    运行之后,页面会显示出来123456;

  • 相关阅读:
    UVA1599 理想路径 Ideal Path(最短路径)
    换根DP
    小w的魔术扑克(树状数组+并查集)
    NOIP 2016蚯蚓(优先队列)
    ZR 动物园
    T105017 seq(DP)
    noip2017酱油记
    noip2017酱油记前篇
    P1985 翻转棋
    luogu P2512 [HAOI2008]糖果传递
  • 原文地址:https://www.cnblogs.com/azedada/p/6857584.html
Copyright © 2011-2022 走看看