• React.js学习(一):设计并实现一个“任务清单列表”


    今日算是React.js学习的第一天,经过昨天前端基本知识的学习,让我能比较顺利的上手React框架。今日实践是一个网页记事清单,由于不是很熟练,任务删除还没写,懒得写的了,做个总结。

    1.React.js 

    这个框架跟Vue.js一样有名气,谁好谁坏我也不知道,但React.js的使用还是比较亲民的。这个框架跟其他框架一样,把页面分割成了一个个的“组件”,React称为“Component”。要实现一个页面,无非是逻辑上将页面模块化,然后实现一个个的模块,最后搭好积木。要使用React,至少需要了解的几个部分:

    1. 属性:state,refs,props
    2. 组件:函数式组件,类组件
    3. jsx语法
    4. 组件的生命周期
    5. 事件处理
    6. 脚手架(提高开发效率)

    2.实现“任务清单”

    任务清单与上一个时钟小案例类似,也是分离js,html,css文件。使用React脚手架,只需要修改App.js,并实现自己的组件即可。

    (1)组件:TodayStamp 今日时间

    1. import React from 'react'
    2. import "./TodoList.css"
    3. class TodayStamp extends React.Component{
    4. render() {
    5. let today = Date.now()
    6. today = new Date(today)
    7. return <div className={"TodayStamp"} {...this.props}>Today: {today.toDateString()}div>
    8. }
    9. }
    10. export default TodayStamp;

    (2)组件: ListForm 清单列表

    1. import React, {createRef} from 'react'
    2. import "./TodoList.css"
    3. class ListForm extends React.Component{
    4. constructor() {
    5. super();
    6. this.state = {counter: 0}
    7. this.toDoListArray = []
    8. this.input = createRef()
    9. this.ul = createRef()
    10. }
    11. render() {
    12. return (
    13. <div>
    14. <form className={"form"} onSubmit={this.dealWithSubmit}>
    15. <input ref={this.input} className="form_input" type="text" id="todo" name="to-do" size="30" required/>
    16. <button className="button"><span>提交span>button>
    17. form>
    18. <div>
    19. <ul ref={this.ul} className="List">
    20. {
    21. this.toDoListArray.map(function (e, idx){
    22. return <li>{idx+1}.{e.text}<br/>创建时间: {e.startTime}li>
    23. })
    24. }
    25. ul>
    26. div>
    27. div>
    28. )
    29. }
    30. dealWithSubmit = (event) => {
    31. event.preventDefault();
    32. // 唯一标识
    33. let itemId = String(Date.now());
    34. // 任务创建时间
    35. let startTime = new Date(Date.now()).toDateString()
    36. console.log(startTime)
    37. // 内容
    38. let todoItem = this.input.current.value;
    39. this.addItemToDOM({"itemID": itemId, "startTime": startTime, "text": todoItem})
    40. }
    41. addItemToDOM = (object)=>{
    42. this.toDoListArray.push(object)
    43. this.setState({counter: this.state.counter+1})
    44. }
    45. }
    46. export default ListForm;

    (3) 组件:任务清单(加入上面两个组件合成)

    1. import React from 'react'
    2. import TodayStamp from "./TodayStamp";
    3. import ListForm from "./ListForm";
    4. import "./TodoList.css"
    5. class TodoList extends React.Component{
    6. render() {
    7. return (<div className={"TodoList"}>
    8. <label className={"TL_title"}>任务清单label>
    9. <ListForm/>
    10. <TodayStamp/>
    11. div>)
    12. }
    13. }
    14. export default TodoList;

     (4)样式表

     样式表使用了一个免费的在线字体,@import的方式导入。

    1. @import url(https://fonts.googleapis.com/css?family=Gochi+Hand);
    2. body{
    3. background-color: rgba(121, 217, 209, 0.86);
    4. min-height: 70vh;
    5. padding: 1rem;
    6. box-sizing: border-box;
    7. display: flex;
    8. justify-content: center;
    9. align-items: center;
    10. color: #494a4b;
    11. font-family: "Gochi Hand", cursive;
    12. text-align: center;
    13. font-size: 130%;
    14. }
    15. .TL_title{
    16. font-family: 'fangsong';
    17. font-weight: bold;
    18. justify-content: center;
    19. margin-bottom: 1rem;
    20. user-select:none;
    21. }
    22. .TodoList{
    23. position: relative;
    24. background: #a9c6ce;
    25. background-image:
    26. linear-gradient(rgba(255,255,255,.3) 1px, transparent 0),
    27. linear-gradient(90deg, rgba(255,255,255,.3) 1px, transparent 0),
    28. linear-gradient(white 1px, transparent 0),
    29. linear-gradient(90deg, white 1px, transparent 0);
    30. display: flow;
    31. justify-content: center;
    32. align-items: center;
    33. width: 100%;
    34. height: auto;
    35. min-height: 500px;
    36. max-width: 500px;
    37. min-width: 250px;
    38. background-size: 25px 25px;
    39. border-radius: 20px;
    40. box-shadow: 4px 3px 7px 2px #000000;
    41. padding: 1rem;
    42. box-sizing: border-box;
    43. }
    44. .TodayStamp{
    45. position: absolute;
    46. bottom: 0;
    47. font-size: 2pt;
    48. font-family: "Gochi Hand", cursive;
    49. border-top-width: 1px;
    50. border-top-color: black;
    51. border-top-style: dashed;
    52. user-select:none;
    53. }
    54. .form_input {
    55. background-color: transparent;
    56. border: none;
    57. border-bottom: dashed 3px #95a9ea;
    58. font-family: "Gochi Hand", cursive;
    59. font-size: 1rem;
    60. color: rgba(63, 62, 65, 0.7);
    61. width: 70%;
    62. margin: auto;
    63. }
    64. .form_input:focus {
    65. outline: none;
    66. border: solid 3px #95a9ea;
    67. }
    68. .button {
    69. position: absolute;
    70. border: black;
    71. transform: rotate(4deg);
    72. transform-origin: center;
    73. font-family: "SansSerif", cursive;
    74. font-weight: bold;
    75. text-decoration: none;
    76. padding: 0 0 4px;
    77. border-radius: 5px;
    78. box-shadow: 0 2px 0 #c5cce7;
    79. transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    80. background-color: rgba(231, 236, 243, 0.7);
    81. margin: auto;
    82. }
    83. .List li{
    84. position: relative;
    85. left: 0;
    86. font-size: 1rem;
    87. text-align:left;
    88. font-weight: bold;
    89. }

     

     

  • 相关阅读:
    虹科教您 | 虹科TSN配置软件RELY-TSN-Configurator基本操作指南
    Java虚拟机栈
    Unity 之 Invoke 与InvokeRepeting 函数控制定时调用
    多测师肖sir_高级讲师_python安装和pycharm(002)
    .net core微服务之服务发现
    【etcd】的限流设计
    arduino 复习题
    挑战来了!如何应对大商家订单多小商家没有订单的数据倾斜问题?
    推荐一款专用低代码工具,一天开发一个系统不是梦
    No ‘Access-Control-Allow-Origin‘ header前端浏览器跨域用LiveServer处理
  • 原文地址:https://blog.csdn.net/qq_42418728/article/details/126961109