react 入门

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. // const element=<h1>dfdf</h1>
  14. const element=(
  15. <div>
  16. <p>title</p>
  17. <p>content</p>
  18. </div>
  19. )
  20. function Welcome(props){
  21. // return <h1>hello,{props.name}</h1>
  22. return (
  23. <div>
  24. <h1>hello,{props.name}</h1>
  25. <h1>hello,{props.name}</h1>
  26. </div>
  27. )
  28. }
  29. const Welcome2=(props)=>{
  30. return <h1>hello,{props.name}</h1>
  31. }
  32. class Wel extends React.Component{ // 都大写
  33. render(){
  34. // return <h1>hi,{this.props.name}</h1>
  35. return (
  36. <div>
  37. <h1>hi,{this.props.name}</h1>
  38. <h1>fine,thank you</h1>
  39. </div>
  40. )
  41. }
  42. }
  43. ReactDOM.render(
  44. // <h1>Hello, !</h1>, // 1
  45. // element, // 2
  46. // <Welcome name={'hehe'}/>, // 函数式组件 3
  47. // <Welcome2 name={'xiaoming2'}/>,
  48. <Wel name={'how are you'}/>, // 类组件 4
  49. document.getElementById('root')
  50. );
  51. </script>
  52. </body>
  53. </html>

state 状态   componentDidMount   componentWillUnmount

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. class Wel extends React.Component {
  14. constructor (props) { // 构造函数
  15. super(props);
  16. this.state = {
  17. date: new Date(),
  18. isToggle:true
  19. }
  20. // 这个绑定是必要的,使`this`在回调中起作用
  21. this.handleClick=this.handleClick.bind(this)
  22.  
  23. }
  24. handleClick(){
  25. this.setState((prevState)=>({ // 箭头函数多了一个 ()
  26. isToggle:!prevState.isToggle // 修改上一次的 state 要传成函数
  27. }))
  28. }
  29. componentDidMount () { // 生命周期 初始化时
  30. this.timeID = setInterval(() => {
  31. this.setState({ // 修改 state 默认传对象
  32. date: new Date()
  33. })
  34. }, 1000)
  35. }
  36.  
  37. componentWillUnmount () { // 页面卸载时
  38. clearInterval(this.timeID)
  39. }
  40.  
  41. render () {
  42. return (
  43. <div>
  44. <h1>hi,{this.state.date.toLocaleTimeString()}</h1>
  45. <button onClick={this.handleClick}> {/*注释也要大括号 onClick 大写*/}
  46. {this.state.isToggle?'on':'off'}
  47. </button>
  48. </div>
  49. )
  50. }
  51. }
  52.  
  53. ReactDOM.render(
  54. <Wel/>,
  55. document.getElementById('root')
  56. );
  57. </script>
  58. </body>
  59. </html>

state demo

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. // 其他都是函数式组件
  14. function Mes (props) {
  15. /*if (props.isToggle) {
  16. return <p>ok</p>
  17. }
  18. return <p>no</p>*/ //
  19.  
  20. // return props.isToggle?<p>okok</p>:<p>nono</p> // 2
  21. const imgs=['http://placekitten.com/200/198','http://placekitten.com/200/160']
  22. return ( //
  23. <div>
  24. <div>{props.isToggle?'okokok':'nonono'}</div>
  25. <img src={props.isToggle?imgs[0]:imgs[1]} alt=""/>
  26. </div>
  27. )
  28. }
  29. // 只有一个写成类组件
  30. class Wel extends React.Component {
  31. constructor (props) {
  32. super(props);
  33. this.state = {
  34. isToggle: true
  35. }
  36. this.handleClick = this.handleClick.bind(this)
  37. }
  38.  
  39. handleClick () {
  40. this.setState((prevState) => ({
  41. isToggle: !prevState.isToggle
  42. }))
  43. }
  44.  
  45. render () {
  46. return (
  47. <div>
  48. <button onClick={this.handleClick}>
  49. {this.state.isToggle ? 'on' : 'off'}
  50. </button>
  51. <Mes isToggle={this.state.isToggle}/> {/*也可以不写成组件,直接丢进来*/}
  52. </div>
  53. )
  54. }
  55. }
  56.  
  57. ReactDOM.render(
  58. <Wel/>,
  59. document.getElementById('root')
  60. );
  61. </script>
  62. </body>
  63. </html>

表单双向数据绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. class Wel extends React.Component {
  14. constructor (props){
  15. super(props)
  16. this.state={
  17. val:''
  18. }
  19. this.change=this.change.bind(this)
  20. }
  21. change(e){
  22. this.setState({
  23. val:e.target.value // 使用e.target
  24. })
  25. }
  26. render () {
  27. return (
  28. <div>
  29. <p>双向数据绑定,太麻烦了,使用 onChange </p>
  30. <input type="text" value={this.state.val} onChange={this.change}/><br/>
  31. {this.state.val}
  32. </div>
  33. )
  34. }
  35. }
  36.  
  37. ReactDOM.render(
  38. <Wel/>,
  39. document.getElementById('root')
  40. );
  41. </script>
  42. </body>
  43. </html>

状态提升前 ,渲染多个

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>状态提前前 渲染多个input </title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. function solve (val) {
  14. if (val) {
  15. return parseInt(val) * 2
  16. }
  17. }
  18. class Wel extends React.Component {
  19. constructor (props) {
  20. super(props)
  21. this.state = {
  22. cel: '',
  23. }
  24. this.change = this.change.bind(this)
  25. }
  26.  
  27. change (e) {
  28. this.setState({
  29. cel: e.target.value
  30. })
  31. }
  32.  
  33. render () {
  34. const name=this.props.name
  35. return (
  36. <div>
  37. {name}: <input type="text" value={this.state.cel} onChange={this.change}/><br/>
  38. {solve(this.state.cel)}
  39. <p>{name} is {parseInt(this.state.cel) > 100 ? 'ok' : 'error'}</p>
  40.  
  41. </div>
  42. )
  43. }
  44. }
  45.  
  46. function All () { // 渲染多个
  47. return (
  48. <div>
  49. <Wel name='one'/>
  50. <Wel name='two'/>
  51. </div>
  52. )
  53. }
  54.  
  55. ReactDOM.render(
  56. <All/>,
  57. document.getElementById('root')
  58. );
  59.  
  60. </script>
  61. </body>
  62. </html>

状态提升,把父组件的state 通过 props 传给子组件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>状态提升 </title>
  6. <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script>
  7. <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script>
  8. <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="root"></div>
  12. <script type="text/babel">
  13. class Wel extends React.Component {
  14. constructor (props) {
  15. super(props)
  16. this.change = this.change.bind(this)
  17. }
  18.  
  19. change (e) {
  20. this.props.celChange(e.target.value) // 调父组件的方法 celChange,把值传过去 {/* 状态提升*/}
  21. }
  22.  
  23. render () {
  24. const name = this.props.name
  25. return (
  26. <div> {/* 状态提升 ,state 变成 props*/}
  27. {name}: <input type="text" value={this.props.cel} onChange={this.change}/><br/>
  28. {this.props.name}{this.props.cel}
  29. {<p>{name} is {parseInt(this.props.cel) > 100 ? 'ok' : 'error'}</p>}
  30. </div>
  31. )
  32. }
  33. }
  34.  
  35. class All extends React.Component {
  36. constructor (props) {
  37. super(props);
  38. this.state = { // 父组件有 state
  39. one: '',
  40. two: ''
  41. }
  42. this.oneFn = this.oneFn.bind(this)
  43. this.twoFn = this.twoFn.bind(this)
  44. }
  45.  
  46. oneFn (val) { // 接收传过来的值
  47. this.setState({
  48. one: val
  49. })
  50. }
  51.  
  52. twoFn (val) {
  53. this.setState({
  54. two: val
  55. })
  56. }
  57.  
  58. render () {
  59. return (
  60. <div>
  61. <Wel name='one' celChange={this.oneFn} cel={this.state.one}/> {/* 把状态 通过 props 传给组件*/}
  62. <Wel name='two' celChange={this.twoFn} cel={this.state.two}/>
  63. {this.state.one && this.state.two && 'this result is: ' + [parseInt(this.state.one) + parseInt(this.state.two)]}
  64. </div>
  65. )
  66. }
  67. }
  68.  
  69. ReactDOM.render(
  70. <All/>,
  71. document.getElementById('root')
  72. );
  73.  
  74. </script>
  75. </body>
  76. </html>

react

  1. react
  2. npx create-react-app my-app
  3.  
  4. npm run eject 暴露webpack配置,默认隐藏
  5.  
  6. npm i -g serve
  7. serve -s build // 静态服务器,打包后使用
  8.  
  9. 函数式组件
  10. function Welcome(props) {
  11. return <h1>Hello, {props.name}</h1>;
  12. }
  13.  
  14. 类组件 【类允许我们在其中添加本地状态(state)和生命周期钩子】
  15. class Welcome extends React.Component {
  16. render() {
  17. return <h1>Hello, {this.props.name}</h1>;
  18. }
  19. }
  20.  
  21. === 函数式组件 使用
  22. function Welcome(props) { // 组件名 W 大名
  23. return <h1>Hello, {props.name}</h1>;
  24. }
  25.  
  26. const element = <Welcome name="Sara" />;
  27. ReactDOM.render(
  28. element,
  29. document.getElementById('root')
  30. );
  31. ======= end =======
  32.  
  33. JavaScript 中, true && expression 总是会评估为 expression ,而 false && expression 总是执行为 false

react 20180504的更多相关文章

  1. react组件的生命周期

    写在前面: 阅读了多遍文章之后,自己总结了一个.一遍加强记忆,和日后回顾. 一.实例化(初始化) var Button = React.createClass({ getInitialState: f ...

  2. 十分钟介绍mobx与react

    原文地址:https://mobxjs.github.io/mobx/getting-started.html 写在前面:本人英语水平有限,主要是写给自己看的,若有哪位同学看到了有问题的地方,请为我指 ...

  3. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  4. React 入门教程

    React 起源于Facebook内部项目,是一个用来构建用户界面的 javascript 库,相当于MVC架构中的V层框架,与市面上其他框架不同的是,React 把每一个组件当成了一个状态机,组件内 ...

  5. 通往全栈工程师的捷径 —— react

    腾讯Bugly特约作者: 左明 首先,我们来看看 React 在世界范围的热度趋势,下图是关键词“房价”和 “React” 在 Google Trends 上的搜索量对比,蓝色的是 React,红色的 ...

  6. 2017-1-5 天气雨 React 学习笔记

    官方example 中basic-click-counter <script type="text/babel"> var Counter = React.create ...

  7. RxJS + Redux + React = Amazing!(译二)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...

  8. React在开发中的常用结构以及功能详解

    一.React什么算法,什么虚拟DOM,什么核心内容网上一大堆,请自行google. 但是能把算法说清楚,虚拟DOM说清楚的聊聊无几.对开发又没卵用,还不如来点干货看看咋用. 二.结构如下: impo ...

  9. React的使用与JSX的转换

    前置技能:Chrome浏览器   一.拿糖:React的使用 React v0.14 RC 发布,主要更新项目: 两个包: React 和 React DOM DOM node refs 无状态的功能 ...

随机推荐

  1. Kickstart 和 Cobbler ks.cfg文件详解

    ks.cfg文件组成大致分为3段 命令段 键盘类型,语言,安装方式等系统的配置,有必选项和可选项,如果缺少某项必选项,安装时会中断并提示用户选择此项的选项 软件包段 %packages @groupn ...

  2. Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定

    一.概述 注解式控制器支持: 请求的映射和限定 参数的自动绑定 参数的注解绑定 二.请求的映射和限定 http请求信息包含六部分信息: ①请求方法: ②URL: ③协议及版本: ④请求头信息(包括Co ...

  3. AVL树探秘

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev) ,专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫. 一.AV ...

  4. 【LOJ6060】【2017 山东一轮集训 Day1 / SDWC2018 Day1】Set 线性基

    题目大意 给出 \(n\) 个非负整数,将数划分成两个集合,记为一号集合和二号集合.\(x_1\) 为一号集合中所有数的异或和,\(x_2\) 为二号集合中所有数的异或和.在最大化 \(x_1 + x ...

  5. 【CF1151E】Number of Components

    [CF1151E]Number of Components 题面 CF 题解 联通块个数=点数-边数. 然后把边全部挂在较小的权值上. 考虑从小往大枚举左端点,等价于每次删掉一个元素,那么删去点数,加 ...

  6. [Ynoi2018]五彩斑斓的世界

    题目描述 二阶堂真红给了你一个长为n的序列a,有m次操作 1.把区间[l,r]中大于x的数减去x 2.查询区间[l,r]中x的出现次数 题解 做YNOI真**爽... 我们发现这道题的操作非常诡异,把 ...

  7. 「LibreOJ NOI Round #1」验题

    麻烦的动态DP写了2天 简化题意:给树,求比给定独立集字典序大k的独立集是哪一个 主要思路: k排名都是类似二分的按位确定过程. 字典序比较本质是LCP下一位,故枚举LCP,看多出来了多少个独立集,然 ...

  8. (转)如何阅读OpenStack源码

    1 关于该项目 本项目使用在线绘图工具web sequencediagrams完成,目标是图形化OpenStack的所有操作流程,通过操作序列图能快速学习Openstack的工作原理,理清各个组件的关 ...

  9. Entity Framework入门教程(17)---记录和拦截数据库命令

    记录和拦截数据库命令 这一节介绍EF6怎么记录和拦截发送给数据库的查询和操作命令. 1.记录EF发送给数据库命令(DbContext.Database.Log) 以前给了查看EF发送给数据库的命令我们 ...

  10. WPF布局的应用

    一 写在开头1.1 本文内容本文主要内容是使用WPF来实现几个简单的界面. 二 登录窗口小例子2.1 实现代码XAML代码: <Window x:Class="LoginDialog. ...