摘要:根据页面中中的例子汇总修改而成。代码代码流逝时间计时器按钮点击计数流逝时间计时器写法实时求解一元二次方程调用组件制作动画效果代码仅在例中使用
根据http://reactjs.cn/react/docs/...页面中Starter Kit 15.3.1中的例子汇总修改而成。
HTML代码
React test
JS代码
const container1 = document.getElementById("container1") const container2 = document.getElementById("container2") const container3 = document.getElementById("container3") const container4 = document.getElementById("container4") const container5 = document.getElementById("container5") const container6 = document.getElementById("container6") //1 "Hello" + name function Welcome(props) { returnHello, { props.name } < /h1>; } const element = < Welcome name = "Diary" / > ; ReactDOM.render( element, container1 ); //2 流逝时间计时器 var Elapsed = React.createClass({ render: function () { var elapsed = Math.round(this.props.elapsed / 100); var seconds = elapsed / 10 + (elapsed % 10 ? "" : ".0"); var message = "React has been successfully running for " + seconds + " seconds."; return
{ message } < /p>; } }); var start = new Date().getTime(); setInterval(function () { ReactDOM.render( < Elapsed elapsed = { new Date().getTime() - start } / > , container2 ); }, 50); //3 按钮点击计数 var Counter = React.createClass({ getInitialState: function () { return { count: 2, sum: 100 }; }, handleClick: function () { this.setState({ count: this.state.count * this.state.count, }); }, handleDoubleClick: function () { this.setState({ count: 2, sum: 1000 }) }, render: function () { return ( < button onClick = { this.handleClick } onDoubleClick = { this.handleDoubleClick }> sum - count: { this.state.sum - this.state.count } < /button> ); } }); ReactDOM.render( < Counter / > , container3 ); //4 流逝时间计时器(ES6写法) class ExampleApplication extends React.Component { render() { var elapsed = Math.round(this.props.elapsed / 100); var seconds = elapsed / 10 + (elapsed % 10 ? "" : ".0"); var message = `React has been successfully running for ${seconds} seconds.`; return
{ message } < /p>; } } var start = new Date().getTime(); setInterval(() => { ReactDOM.render( < ExampleApplication elapsed = { new Date().getTime() - start }/>, container4 ); }, 50); //5 实时求解一元二次方程 var QuadraticCalculator = React.createClass({ getInitialState: function() { return { a: 1, b: 3, c: -4 }; }, /** * This function will be re-bound in render multiple times. Each .bind() will * create a new function that calls this with the appropriate key as well as * the event. The key is the key in the state object that the value should be * mapped from. */ handleInputChange: function(key, event) { var partialState = {}; partialState[key] = parseFloat(event.target.value); this.setState(partialState); }, render: function() { var a = this.state.a; var b = this.state.b; var c = this.state.c; var root = Math.sqrt(Math.pow(b, 2) - 4 * a * c); var denominator = 2 * a; var x1 = (-b + root) / denominator; var x2 = (-b - root) / denominator; return (
ax2 + bx + c = 0); } }); ReactDOM.render(Solve for x:
x: {x1}, {x2}, container5 ); //6 调用React组件CSSTransitionGroup制作动画效果 var CSSTransitionGroup = React.addons.CSSTransitionGroup; var INTERVAL = 2000; var AnimateDemo = React.createClass({ getInitialState: function() { return {current: 0}; }, componentDidMount: function() { this.interval = setInterval(this.tick, INTERVAL); }, componentWillUnmount: function() { clearInterval(this.interval); }, tick: function() { this.setState({current: this.state.current + 1}); }, render: function() { var children = []; var colors = ["#fac", "#cdc", "#36d", "#ca0", "#0aa"]; for (var i = this.state.current, pos=0; i < this.state.current + colors.length; i++, pos++) { var style = { left: pos * 128, background: colors[i % colors.length] }; children.push( {i}); } return ({children} ); } }); ReactDOM.render(, container6 );
CSS代码(仅在例6中使用)
.example-enter, .example-leave { -webkit-transition: all .25s; transition: all .25s; } .example-enter, .example-leave.example-leave-active { opacity: 0.01; } .example-leave.example-leave-active { margin-left: -128px; } .example-enter { margin-left: 128px; } .example-enter.example-enter-active, .example-leave { margin-left: 0; opacity: 1; } .animateExample { display: block; height: 128px; position: relative; width: 384px; } .animateItem { color: white; font-size: 36px; font-weight: bold; height: 128px; line-height: 128px; position: absolute; text-align: center; -webkit-transition: all .25s; /* TODO: make this a move animation */ transition: all .25s; /* TODO: make this a move animation */ width: 128px; }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/81877.html
摘要:服务端渲染的手脚架这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于已经提供代码热加载,使用预处理,单元测试,代码覆盖率报告,代码分割等等更多。 Universal React Starter Kit 服务端渲染的React手脚架 这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于webpack已经提供代码热加载,使用...
摘要:服务端渲染的手脚架这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于已经提供代码热加载,使用预处理,单元测试,代码覆盖率报告,代码分割等等更多。 Universal React Starter Kit 服务端渲染的React手脚架 这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于webpack已经提供代码热加载,使用...
摘要:服务端渲染的手脚架这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于已经提供代码热加载,使用预处理,单元测试,代码覆盖率报告,代码分割等等更多。 Universal React Starter Kit 服务端渲染的React手脚架 这个启动包的设计是为了让你使用一整套最新最酷的前端技术,所有都是可配置,富特性,基于webpack已经提供代码热加载,使用...
阅读 2500·2021-11-22 09:34
阅读 3512·2021-11-15 11:37
阅读 2310·2021-09-13 10:37
阅读 2064·2021-09-04 16:40
阅读 1381·2021-09-02 15:40
阅读 2430·2019-08-30 13:14
阅读 3287·2019-08-29 13:42
阅读 1828·2019-08-29 13:02