zoukankan      html  css  js  c++  java
  • reactNative中的props和state传值

    reactNative中的props和state传值

      reactNative项目由不同的组件组成,每个组件都有state和props,

      组件内部传值可以通过state和props传值,外部调用向组件传值需通过

      props进行传值。

      案例实现:实现一个动画效果,打开网页,文字从左向右平移200px;

          代码实现:

          01.html:

      <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <script src="react.js"></script>
        <script src="react-dom.js"></script>
        <script src="react-with-addons.js"></script>
        <script src="babel.min.js"></script>
        <script type="text/babel" src="01.js"></script>
        <style type="text/css">
        
        </style>
        <title>React</title>
        </head>
        <body>
        </body>
        </html>

      01.js:

        let MyComponent=React.createClass({

             //默认熟悉,通过属性配置动画

           getDefaultProps:function(){

             return{

               position:100;//px  平移的距离

                 time:10    //ms频率,10ms移动一次

             };

           },

           getIniniaState:function(){

             return {position:0};

           },

           render:function(){

             let style={

                marginLeft:this.state.position

             };

             return <div style={style}>This will animated</div>

           },

        });

        //动画执行过程,不断改变状态

        transformComponent:function(){

          if(this.state.position<this.props.position){

            //state改变后重新渲染页面

            this.setState({

              postion:++this.state.position

            });

          }else{

            //完成动画,取消定时器

            clearInterval(this.timer);

          

          }

        }    

        //真实的DOM被渲染出来后调用

        componentDidMount:function(){

          if(this.props.position){

            this.timer=setInterval(this.transformComponent,this.props.time);

          }

        }

        ReactDOM.render(<MyComponent postion={200} time={5} />,document.body);

     

             

  • 相关阅读:
    作业五:团队项目——项目启动及需求分析
    结对编程项目---四则运算
    PSP记录个人项目耗时情况
    代码复查
    是否需要有代码规范?
    编写一个能自动生成小学四则运算题目的程序。
    目前流行的源程序版本管理软件和项目管理软件的优缺点
    在Github注册账户
    浏览完整部教材,列出不懂的5-10个问题
    FZU 1683 纪念SlingShot(矩阵水)
  • 原文地址:https://www.cnblogs.com/qqpw/p/6633582.html
Copyright © 2011-2022 走看看