-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xml
7189 lines (7112 loc) · 657 KB
/
index.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>cengdong的技术博客</title>
<link>https://www.cengdong.store/</link>
<description>Recent content on cengdong的技术博客</description>
<generator>Hugo -- gohugo.io</generator>
<language>zh-cn</language>
<copyright>Cengdong</copyright>
<lastBuildDate>Thu, 28 Nov 2024 22:09:23 +0800</lastBuildDate><atom:link href="https://www.cengdong.store/index.xml" rel="self" type="application/rss+xml" /><item>
<title>【CSS】css属性计算过程</title>
<link>https://www.cengdong.store/p/csscss%E5%B1%9E%E6%80%A7%E8%AE%A1%E7%AE%97%E8%BF%87%E7%A8%8B/</link>
<pubDate>Thu, 28 Nov 2024 22:09:23 +0800</pubDate>
<guid>https://www.cengdong.store/p/csscss%E5%B1%9E%E6%80%A7%E8%AE%A1%E7%AE%97%E8%BF%87%E7%A8%8B/</guid>
<description><h3 id="css-属性计算过程">CSS 属性计算过程</h3>
<p>CSS 属性计算过程是指元素从所有 CSS 属性都没有值到所有 CSS 属性都有值,中间经历的过程。</p>
<p>CSS 属性计算过程可以分为以下几个步骤:</p>
<ul>
<li>确定声明值</li>
<li>层叠</li>
<li>继承</li>
<li>使用默认值</li>
</ul>
<h3 id="确定声明值">确定声明值</h3>
<p>关注两个样式表:</p>
<ul>
<li>作者样式表</li>
<li>浏览器默认样式表</li>
</ul>
<p>找到这两个样式表中<code>没有冲突</code>的样式,直接作为计算后的样式</p>
<h3 id="层叠">层叠</h3>
<p>层叠的样式经过层叠规则确定样式</p>
<h3 id="继承">继承</h3>
<p>有些样式通过继承获得,如字体、颜色、边框、背景等</p>
<h3 id="使用默认值">使用默认值</h3>
<p>如果没有其他样式可以继承,则使用浏览器默认样式</p>
</description>
</item>
<item>
<title>【CSS】css层叠规则</title>
<link>https://www.cengdong.store/p/csscss%E5%B1%82%E5%8F%A0%E8%A7%84%E5%88%99/</link>
<pubDate>Thu, 28 Nov 2024 21:56:37 +0800</pubDate>
<guid>https://www.cengdong.store/p/csscss%E5%B1%82%E5%8F%A0%E8%A7%84%E5%88%99/</guid>
<description><h3 id="层叠规则">层叠规则</h3>
<ul>
<li>比较重要性</li>
<li>比较特殊性</li>
<li>比较源次序</li>
</ul>
<h4 id="重要性">重要性</h4>
<ul>
<li>带有 important 的作者样式</li>
<li>带有 important 的默认样式</li>
<li>作者样式</li>
<li>浏览器默认样式</li>
</ul>
<h4 id="特殊性">特殊性</h4>
<p>特殊性是指样式的具体性质,具体来说,就是样式的来源和权重。</p>
<p>对每个样式分别计数:</p>
<ul>
<li>style 是否内联</li>
<li>id</li>
<li>属性/类/伪类</li>
<li>元素/伪元素</li>
</ul>
<h4 id="源次序">源次序</h4>
<p>后面覆盖前面,后面的样式会覆盖前面的样式。</p>
</description>
</item>
<item>
<title>【English】英语语法学习笔记</title>
<link>https://www.cengdong.store/p/english%E8%8B%B1%E8%AF%AD%E8%AF%AD%E6%B3%95%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</link>
<pubDate>Tue, 23 Jul 2024 21:18:47 +0800</pubDate>
<guid>https://www.cengdong.store/p/english%E8%8B%B1%E8%AF%AD%E8%AF%AD%E6%B3%95%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</guid>
<description><h2 id="冠词">冠词</h2>
<h3 id="冠词的分类">冠词的分类</h3>
<ul>
<li>不定冠词:a/an</li>
<li>定冠词:the</li>
<li>零冠词:无冠词</li>
</ul>
<h3 id="冠词的用法">冠词的用法</h3>
<ul>
<li>不定冠词:
<ul>
<li>泛指用法: I saw a rabbit</li>
<li>表示数字&quot;1&quot;: an apple</li>
<li>表示&quot;每&quot;: a day = every day</li>
<li>主谓一致: a teacher and a video creator</li>
<li>固定表达:have a good time; as a result; in a hurry; all of a sudden;</li>
</ul>
</li>
<li>定冠词:
<ul>
<li>特指用法: I saw a rabbit, the rabbit ran fast.</li>
<li>泛指用法: 西洋古典乐器:play the piano;</li>
<li>在形容词前:the beautiful girl</li>
<li>在序数词前/最高级:the first time</li>
<li>固定表达:all the time; by the way; in the end; out of the question;</li>
</ul>
</li>
<li>零冠词:
<ul>
<li>复数泛指: rabbits are smart</li>
<li>专有名词: Papa Rabbit</li>
<li>泛指概念/复数: Summer is hot</li>
<li>固定表达:fish and chips; bread and butter; tea and coffee;</li>
</ul>
</li>
</ul>
<h2 id="动词">动词</h2>
<h3 id="实义动词">实义动词</h3>
<ul>
<li>按照动作承受者划分:
<ul>
<li>及物动词:动词后面需要加宾语,表示动作的承受者。</li>
<li>不及物动词:动词后面不需要加宾语,表示动作的执行者。</li>
</ul>
</li>
<li>按照动作状态划分:
<ul>
<li>动作动词</li>
<li>感官动词</li>
</ul>
</li>
</ul>
<h3 id="系动词">系动词</h3>
<p>系动词的作用就是给系动词前的<code>对象赋值</code></p>
<ul>
<li>be</li>
<li>感官动词:looks, tastes, smells, sounds, feels</li>
<li>一直:remained</li>
<li>慢慢增长: grrew</li>
<li>变得:became</li>
<li>保持:remained</li>
<li>持续:continued</li>
<li>消失:disappeared</li>
</ul>
<h3 id="助动词">助动词</h3>
<ul>
<li>按照动作状态划分:
<ul>
<li>短时助动词:表示动作的执行者正在执行某种动作。</li>
<li>长时助动词:表示动作的执行者已经完成了某种动作。</li>
</ul>
</li>
<li>按照动作状态划分:
<ul>
<li>短时助动词:表示动作的执行者正在执行某种动作。</li>
<li>长时助动词:表示动作的执行者已经完成了某种动作。</li>
</ul>
</li>
</ul>
<h3 id="情态动词">情态动词</h3>
<ul>
<li>按照动作状态划分:
<ul>
<li>短时情态动词:表示动作的执行者正在执行某种动作。</li>
<li>长时情态动词:表示动作的执行者已经完成了某种动作。</li>
</ul>
</li>
<li>按照动作状态划分:
<ul>
<li>短时情态动词:表示动作的执行者正在执行某种动作。</li>
<li>长时情态动词:表示动作的执行者已经完成了某种动作。</li>
</ul>
</li>
</ul>
<h3 id="非谓语动词">非谓语动词</h3>
<ul>
<li>不定式</li>
<li>动名词</li>
<li>现在分词</li>
<li>过去分词</li>
</ul>
<h2 id="名词">名词</h2>
<h3 id="名词的分类">名词的分类</h3>
<ul>
<li>普通名词</li>
<li>专有名词</li>
<li>动名词</li>
<li>形容词</li>
<li>数词</li>
<li>介词</li>
<li>代词</li>
<li>连词</li>
</ul>
<h3 id="介词">介词</h3>
<p>放置在前面的词就叫介词:preposition + 名词/代词</p>
<p>介词组成:</p>
<ul>
<li>简单介词: in; on; at; over;</li>
<li>介词短语:by way of; at the end of; along with; because of; previous to;</li>
<li>合成介词:into, onto, within</li>
<li>分词介词</li>
</ul>
<p>按照作用划分:</p>
<ul>
<li>时间介词:in, on, at, over, above, under, below, near, by/beside, next to, across, through, past, in front of, before, behind, between &hellip;and &hellip;, among, round, around, along, to , towards, onto, into, out of</li>
<li>空间介词:in, on, at, during, for, since, before, after, from &hellip; to &hellip;, by, until</li>
<li>方式介词:in, through, by, with, on</li>
<li>原因介词:for, with, because/ because of, due to, as a result of, in order to, in the hope of, in case of</li>
<li>关于介词:about, concerning, on, in, of, for, to, with, from, by</li>
<li>数值介词:at, by, for</li>
<li>目的介词:for, in order to, to, in the hope of, in case of</li>
<li>条件介词:if, unless, in case of, in the event of, in the face of</li>
<li>比较介词:with, against, compared with, in comparison with, in contrast to, in the same way as, like, as, as if, as though</li>
</ul>
<h3 id="代词">代词</h3>
<p>代词分类:</p>
<ul>
<li>人称代词:I, you, he, she, we, they</li>
<li>人称代词宾格:me, you, him, her, us, them</li>
<li>形容词性物主代词:my, your, his, her, our, their</li>
<li>名词性物主代词:mine, yours, his, hers, ours, theirs</li>
<li>反身代词:myself, yourself, himself, herself, ourselves, themselves</li>
<li>强调代词:myself, yourself, himself, herself, ourselves, themselves</li>
<li>指示代词:this, that, these, those</li>
<li>相互代词:each other, one another</li>
<li>不定代词:some, any, every, all, none, much, many, few, little</li>
<li>疑问代词:who, what, where, when, why, how</li>
<li>连词代词:either, neither, both, all, none, some, any, each, every</li>
<li>关系代词:who, which, that</li>
</ul>
</description>
</item>
<item>
<title>【React】React基础入门</title>
<link>https://www.cengdong.store/p/reactreact%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8/</link>
<pubDate>Fri, 12 Jul 2024 10:23:54 +0800</pubDate>
<guid>https://www.cengdong.store/p/reactreact%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8/</guid>
<description><h2 id="react-开发环境搭建">React 开发环境搭建</h2>
<ul>
<li>安装 nodejs:官网下载最近 LTS 版本</li>
<li>安装集成开发环境:vscode</li>
<li>创建 react 项目:
<ul>
<li>npx create-react-app project-name</li>
<li>npm create vite@latest</li>
</ul>
</li>
</ul>
<h2 id="react-核心语法jsx">React 核心语法:JSX</h2>
<p><code>JSX</code>是 Javascript 和 Html 的缩写,表示<code>在js代码中编写html模版</code></p>
<p><code>JSX</code>不能直接在浏览器中运行,需要<code>bable</code>进行编码成纯 js 代码</p>
<h2 id="jsx-基础">JSX 基础</h2>
<h3 id="jsx-识别-js-表达式">JSX 识别 js 表达式</h3>
<p>JSX 通过<code>{}</code>语法可以识别 js 表达式</p>
<p>可运行以下 js 表达式:</p>
<ul>
<li>字符串:{&ldquo;hello world&rdquo;}(支持模版字符串)</li>
<li>变量/js 语句(运算符)</li>
<li>函数调用</li>
<li>方法调用</li>
</ul>
<h3 id="列表渲染">列表渲染</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">List</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">list</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;python&#39;</span><span class="p">,</span> <span class="s1">&#39;java&#39;</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="nx">list</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">item</span> <span class="p">=&gt;</span> <span class="p">&lt;</span><span class="nt">li</span> <span class="na">key</span><span class="o">=</span><span class="p">{</span><span class="nx">item</span><span class="p">.</span><span class="nx">id</span><span class="p">}&gt;{</span><span class="nx">item</span><span class="p">}&lt;/</span><span class="nt">li</span><span class="p">&gt;)</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="简单条件渲染">简单条件渲染</h3>
<p>对于简单的条件渲染,可通过<code>逻辑与运算符</code>或者<code>三目运算符</code>实现</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">isLogin</span> <span class="o">=</span> <span class="kc">true</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">UserInfo</span><span class="p">({</span> <span class="nx">username</span> <span class="p">})</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="nx">isLogin</span> <span class="o">?</span> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span><span class="nx">username</span><span class="o">:</span> <span class="p">{</span><span class="nx">username</span><span class="p">}&lt;/</span><span class="nt">p</span><span class="p">&gt;</span> <span class="o">:</span> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span><span class="nx">请登录</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;/&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="复杂条件渲染">复杂条件渲染</h3>
<p>复杂的条件渲染通过<code>函数</code>实现</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">type</span> <span class="o">=</span> <span class="mi">0</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">showTypedTemplate</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">type</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span><span class="nx">我是type0</span><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">type</span> <span class="o">===</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span><span class="nx">我是type1</span><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="p">(</span><span class="nx">type</span> <span class="o">===</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span><span class="nx">我是type2</span><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nx">func</span> <span class="nx">Article</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="nx">showTypedTemplate</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="react-事件绑定">React 事件绑定</h2>
<h3 id="语法">语法</h3>
<p>on + 事件名称 = {事件处理函数}</p>
<h3 id="使用事件对象">使用事件对象</h3>
<p>在<code>事件处理函数</code>中使用<code>形参e</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">handlerFunc</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">handlerFunc</span><span class="p">}&gt;</span><span class="nx">click</span> <span class="nx">me</span><span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="k">default</span> <span class="nx">App</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="传递参数">传递参数</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">handlerFunc</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;</span><span class="nt">button</span>
</span></span><span class="line"><span class="cl"> <span class="na">onClick</span><span class="o">=</span><span class="p">{(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">handlerFunc</span><span class="p">(</span><span class="s1">&#39;cengdong&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}}</span>
</span></span><span class="line"><span class="cl"> <span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="nx">click</span> <span class="nx">me</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="k">default</span> <span class="nx">App</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="react-组件状态管理">React 组件状态管理</h2>
<p>给 React 组件添加一个<code>状态变量</code>,让组件在不同状态下有<code>不同表现</code></p>
<h3 id="usestate-基本使用">useState 基本使用</h3>
<p>通过 useState hook 函数给组件添加一个计数器变量</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="nx">fucntion</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="p">[</span><span class="nx">state</span><span class="p">,</span> <span class="nx">setState</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">clickHandle</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">setState</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">clickHandle</span><span class="p">}&gt;{</span><span class="nx">state</span><span class="p">}&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="usestate-注意事项">useState 注意事项</h3>
<p>状态变量是不可变的,只能通过<code>setXXX</code>函数,并且<code>传入新的值</code>来改变状态</p>
<p>直接修改状态不能引发视图更新</p>
<h3 id="usestate-案例">useState 案例</h3>
<h4 id="tabl-栏切换">tabl 栏切换</h4>
<ul>
<li>通过 useSate(type)记录 type 状态</li>
<li>通过事件回调函数调用状态修改函数,传递被点击项的 type 值为参数</li>
<li>通过 classnames 插件优雅实现类名切换</li>
</ul>
<h4 id="表单受控绑定">表单受控绑定</h4>
<p>通过表单<code>value</code>属性<code>绑定状态</code>,通过<code>onChange</code>事件属性<code>绑定状态同步的函数</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="p">[</span><span class="nx">content</span><span class="p">,</span> <span class="nx">setContent</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s2">&#34;&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">clickHandle</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">setContent</span><span class="p">(</span><span class="s2">&#34;&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="na">text</span> <span class="na">value</span><span class="o">=</span><span class="p">{</span><span class="nx">content</span><span class="p">}</span> <span class="na">onChange</span><span class="o">=</span><span class="p">{(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">setContent</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">)}/&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="react-获取-dom-元素">React 获取 Dom 元素</h2>
<p>通过 useRef hook 获取 dom 元素从而调用 dom 元素的方法</p>
<h3 id="useref-基本用法">useRef 基本用法</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">App</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">inputRef</span> <span class="o">=</span> <span class="nx">useRef</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">publishHandle</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">inputRef</span><span class="p">.</span><span class="nx">current</span><span class="p">.</span><span class="nx">focus</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">&lt;</span><span class="nt">input</span> <span class="na">ref</span><span class="o">=</span><span class="p">{</span><span class="nx">inoutRef</span><span class="p">}</span> <span class="na">onClick</span><span class="o">=</span><span class="p">{</span><span class="nx">publichHandle</span><span class="p">}</span> <span class="p">/&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="注意事项">注意事项</h3>
<p>渲染完毕之后,dom 才可用</p>
<h3 id="useref-使用场景">useRef 使用场景</h3>
<ul>
<li>获取 Dom 元素,调用 Dom 元素的属性或者方法</li>
<li>引用一个不需要被渲染的值</li>
<li>ref 不仅可以引用单个 dom 元素,也可以引用一个列表</li>
<li>父组件要获取子组件 Dom 需要 forwardRef 函数做处理,因为 React 框架不允许</li>
</ul>
<h2 id="组件间通信">组件间通信</h2>
<h3 id="父传子">父传子</h3>
<p>实现步骤:</p>
<ul>
<li>父组件传递数据:在子组件标签上<code>绑定属性</code></li>
<li>子组件接收数据:子组件通过<code>props参数</code>接受数据</li>
</ul>
<h4 id="props-说明">props 说明</h4>
<ul>
<li>props 对象包含了父组件传递过来的<code>所有数据</code></li>
<li>props 对象能传递<code>任意类型的数据</code></li>
<li>props 对象是<code>只读对象</code></li>
</ul>
<h4 id="children-说明">children 说明</h4>
<p>在子组件内部还可以<code>嵌套内容</code>,同样会给<code>子组件传递数据</code>,存在 props 对象<code>children属性</code>上</p>
<h3 id="子传父">子传父</h3>
<ul>
<li>子组件调用<code>父组件传递过来的函数</code></li>
<li>通过<code>函数参数</code>传递数据给父组件</li>
</ul>
<h3 id="兄弟组件通信">兄弟组件通信</h3>
<p>兄弟组件之间通过<code>状态提升</code>传递数据</p>
<h3 id="数据跨层通信">数据跨层通信</h3>
<ul>
<li>使用 <code>createContext</code> 函数创建一个<code>上下文对象 Ctx</code></li>
<li>在顶层组件(App)中通过 <code>Ctx.Provider</code> 组件提供数据</li>
<li>在底层组件(B)中通过 <code>useContext</code> hook 获取消费数据</li>
</ul>
<h2 id="useeffect">useEffect</h2>
<blockquote>
<p>useEffect 是一个 React Hook,它允许你 将组件与外部系统同步</p>
</blockquote>
<h3 id="参考">参考</h3>
<p>useEffect(setup, dependencies?)</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createConnection</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;./chat.js&#39;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">ChatRoom</span><span class="p">({</span> <span class="nx">roomId</span> <span class="p">})</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="p">[</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">setServerUrl</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s1">&#39;https://localhost:1234&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="nx">useEffect</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">connection</span> <span class="o">=</span> <span class="nx">createConnection</span><span class="p">(</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">roomId</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="nx">connection</span><span class="p">.</span><span class="nx">connect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">connection</span><span class="p">.</span><span class="nx">disconnect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">},</span> <span class="p">[</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">roomId</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// ...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>setup:处理 Effect 的函数。setup 函数选择性返回一个 清理(cleanup) 函数。当组件被添加到 DOM 的时候,React 将运行 setup 函数。在每次依赖项变更重新渲染后,React 将首先使用旧值运行 cleanup 函数(如果你提供了该函数),然后使用新值运行 setup 函数。在组件从 DOM 中移除后,React 将最后一次运行 cleanup 函数。</p>
</li>
<li>
<p>可选 dependencies:setup 代码中引用的所有响应式值的列表。响应式值包括 props、state 以及所有直接在组件内部声明的变量和函数。如果你的代码检查工具 配置了 React,那么它将验证是否每个响应式值都被正确地指定为一个依赖项。依赖项列表的元素数量必须是固定的,并且必须像 [dep1, dep2, dep3] 这样内联编写。React 将使用 Object.is 来比较每个依赖项和它先前的值。如果省略此参数,则在每次重新渲染组件之后,将重新运行 Effect 函数。如果你想了解更多,请参见 传递依赖数组、空数组和不传递依赖项之间的区别。</p>
</li>
</ul>
<h3 id="不同依赖项的区别">不同依赖项的区别</h3>
<table>
<thead>
<tr>
<th>依赖项</th>
<th>副作用函数执行时机</th>
</tr>
</thead>
<tbody>
<tr>
<td>没有依赖项</td>
<td>组件初次渲染+组件更新时</td>
</tr>
<tr>
<td>空数组</td>
<td>组件初次渲染</td>
</tr>
<tr>
<td>特定依赖项</td>
<td>组件初次渲染+依赖项变化</td>
</tr>
</tbody>
</table>
<h3 id="注意事项-1">注意事项</h3>
<ul>
<li>useEffect 是一个 Hook,因此只能在 组件的顶层 或自己的 Hook 中调用它,而不能在循环或者条件内部调用。如果需要,抽离出一个新组件并将 state 移入其中。</li>
<li>如果你 没有打算与某个外部系统同步,那么你可能不需要 Effect。</li>
<li>当严格模式启动时,React 将在真正的 setup 函数首次运行前,运行一个开发模式下专有的额外 setup + cleanup 周期。这是一个压力测试,用于确保 cleanup 逻辑“映射”到了 setup 逻辑,并停止或撤消 setup 函数正在做的任何事情。如果这会导致一些问题,请实现 cleanup 函数。</li>
<li>如果你的一些依赖项是组件内部定义的对象或函数,则存在这样的风险,即它们将 导致 Effect 过多地重新运行。要解决这个问题,请删除不必要的 对象 和 函数 依赖项。你还可以 抽离状态更新 和 非响应式的逻辑 到 Effect 之外。</li>
<li>如果你的 Effect 不是由交互(比如点击)引起的,那么 React 会让浏览器 在运行 Effect 前先绘制出更新后的屏幕。如果你的 Effect 正在做一些视觉相关的事情(例如,定位一个 tooltip),并且有显著的延迟(例如,它会闪烁),那么将 useEffect 替换为 useLayoutEffect。</li>
<li>如果你的 Effect 是由一个交互(比如点击)引起的,React 可能会在浏览器重新绘制屏幕之前执行 Effect。通常情况下,这样是符合预期的。但是,如果你必须要推迟 Effect 执行到浏览器绘制之后,和使用 alert() 类似,可以使用 setTimeout。有关更多信息,请参阅 reactwg/react-18/128。</li>
<li>即使你的 Effect 是由一个交互(比如点击)引起的,React 也可能允许浏览器在处理 Effect 内部的状态更新之前重新绘制屏幕。通常,这样是符合预期的。但是,如果你一定要阻止浏览器重新绘制屏幕,则需要用 useLayoutEffect 替换 useEffect。</li>
<li>Effect 只在客户端上运行,在服务端渲染中不会运行。</li>
</ul>
<h3 id="使用场景">使用场景</h3>
<h4 id="连接到外部系统">连接到外部系统</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;react&#39;</span>
</span></span><span class="line"><span class="cl"><span class="kr">import</span> <span class="p">{</span> <span class="nx">createConnection</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;./chat.js&#39;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">ChatRoom</span><span class="p">({</span> <span class="nx">roomId</span> <span class="p">})</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="p">[</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">setServerUrl</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s1">&#39;https://localhost:1234&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"> <span class="nx">useEffect</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">connection</span> <span class="o">=</span> <span class="nx">createConnection</span><span class="p">(</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">roomId</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="nx">connection</span><span class="p">.</span><span class="nx">connect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">connection</span><span class="p">.</span><span class="nx">disconnect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="p">},</span> <span class="p">[</span><span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">roomId</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// ...
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="在自定义-hook-中封装-effect">在自定义 Hook 中封装 Effect</h4>
<p>Effect 是一种<code>脱围机制</code>:当你需要“走出 React 之外”或者当你的使用场景没有更好的内置解决方案时,你可以使用它们。如果你发现自己经常需要手动编写 Effect,那么这通常表明你需要为组件所依赖的通用行为提取一些 自定义 Hook。</p>
<p>例如,这个 useChatRoom 自定义 Hook 把 Effect 的逻辑“隐藏”在一个更具声明性的 API 之后:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-jsx" data-lang="jsx"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">useChatRoom</span><span class="p">({</span> <span class="nx">serverUrl</span><span class="p">,</span> <span class="nx">roomId</span> <span class="p">})</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">useEffect</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">options</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">serverUrl</span><span class="o">:</span> <span class="nx">serverUrl</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="nx">roomId</span><span class="o">:</span> <span class="nx">roomId</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">connection</span> <span class="o">=</span> <span class="nx">createConnection</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="nx">connection</span><span class="p">.</span><span class="nx">connect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="k">return</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="nx">connection</span><span class="p">.</span><span class="nx">disconnect</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"> <span class="p">},</span> <span class="p">[</span><span class="nx">roomId</span><span class="p">,</span> <span class="nx">serverUrl</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></description>
</item>
<item>
<title>【JavaScript】webAPIs六</title>
<link>https://www.cengdong.store/p/javascriptwebapis%E5%85%AD/</link>
<pubDate>Wed, 01 May 2024 22:49:12 +0800</pubDate>
<guid>https://www.cengdong.store/p/javascriptwebapis%E5%85%AD/</guid>
<description><h2 id="正则表达式">正则表达式</h2>
</description>
</item>
<item>
<title>【JavaScript】webAPIs五</title>
<link>https://www.cengdong.store/p/javascriptwebapis%E4%BA%94/</link>
<pubDate>Tue, 30 Apr 2024 05:08:37 +0800</pubDate>
<guid>https://www.cengdong.store/p/javascriptwebapis%E4%BA%94/</guid>
<description><h2 id="window-对象">window 对象</h2>
<h3 id="bom浏览器对象模型">BOM(浏览器对象模型)</h3>
<ul>
<li>BOM(Browser Object Model ) 是浏览器对象模型</li>
<li>window 对象是一个全局对象,也可以说是 JavaScript 中的顶级对象</li>
<li>像 document、alert()、console.log()这些都是 window 的属性,基本 BOM 的属性和方法都是 window 的。</li>
<li>所有通过 var 定义在全局作用域中的变量、函数都会变成 window 对象的属性和方法</li>
<li>window 对象下的属性和方法调用的时候可以省略 window</li>
</ul>
<p>window 对象组成:</p>
<ul>
<li>document</li>
<li>location</li>
<li>navigation</li>
<li>history</li>
<li>screen</li>
</ul>
<h3 id="定时器-延迟函数">定时器-延迟函数</h3>
<p>语法:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">// 设置延迟函数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">let</span> <span class="nx">timerId</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{},</span> <span class="mi">1000</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 清除延迟函数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timerId</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>延迟函数与间歇函数对比:</p>
<ul>
<li>延时函数: 执行一次</li>
<li>间歇函数:每隔一段时间就执行一次,除非手动清除</li>
</ul>
<h3 id="js-执行机制">Js 执行机制</h3>
<ul>
<li>JavaScript 语言的一大特点就是<code>单线程</code></li>
<li>HTML5 提出<code>Web Worker</code>标准,允许 JavaScript 脚本创建<code>多个线程</code>。于是,JS 中出现了同步和异步</li>
<li>同步任务都在主线程上执行,形成一个<code>执行栈</code></li>
<li>异步任务相关添加到<code>任务队列</code>中</li>
<li><code>先执行执行栈</code>中的同步任务,一旦执行栈中的所有同步任务执行完毕,系统就会<code>按次序读取任务队列</code>中的异步任务,于是被读取的异步任务<code>结束等待状态</code>,进入执行栈,开始执行</li>
</ul>
<p>异步任务分类:</p>
<ul>
<li>普通事件,如 click、resize 等</li>
<li>资源加载,如 load、error 等</li>
<li>定时器,包括 setInterval、setTimeout 等</li>
</ul>
<h3 id="location-对象">location 对象</h3>
<p>location 的数据类型是对象,它拆分并保存了 <code>URL 地址</code>的各个组成部分</p>
<p>常用属性和方法:</p>
<ul>
<li>href 属性获取完整的 URL 地址,对其赋值时用于地址的跳转</li>
<li>search 属性获取地址中携带的参数,符号 ?后面部分</li>
<li>hash 属性获取地址中的啥希值,符号 # 后面部分</li>
<li>reload 方法用来刷新当前页面,传入参数 true 时表示强制刷新</li>
</ul>
<h3 id="navigator-对象">navigator 对象</h3>
<p>navigator 的数据类型是对象,该对象下记录了<code>浏览器自身的相关信息</code></p>
<p>常用属性和方法:通过 <code>userAgent</code> 检测浏览器的版本及平台</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">// 检测 userAgent(浏览器信息)
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="o">!</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">userAgent</span> <span class="o">=</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">userAgent</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// 验证是否为Android或iPhone
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="kr">const</span> <span class="nx">android</span> <span class="o">=</span> <span class="nx">userAgent</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/(Android);?[\s\/]+([\d.]+)?/</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="kr">const</span> <span class="nx">iphone</span> <span class="o">=</span> <span class="nx">userAgent</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/(iPhone\sOS)\s([\d_]+)/</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="c1">// 如果是Android或iPhone,则跳转至移动站点
</span></span></span><span class="line"><span class="cl"><span class="c1"></span> <span class="k">if</span> <span class="p">(</span><span class="nx">android</span> <span class="o">||</span> <span class="nx">iphone</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">location</span><span class="p">.</span><span class="nx">href</span> <span class="o">=</span> <span class="s1">&#39;http://m.itcast.cn&#39;</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">})()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="history-对象">history 对象</h3>
<p>history 的数据类型是对象,主要管理历史记录, 该对象与浏览器地址栏的操作相对应,如<code>前进</code>、<code>后退</code>、<code>历史记录</code>等</p>
<p>常用属性和方法:</p>
<table>
<thead>
<tr>
<th>history 对象方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>back()</td>
<td>后退 1 步</td>
</tr>
<tr>
<td>forward()</td>
<td>前进 1 步</td>
</tr>
<tr>
<td>go(步数)</td>
<td>正数前进,负数后退</td>
</tr>
</tbody>
</table>
<h2 id="本地存储">本地存储</h2>
<h3 id="本地存储介绍">本地存储介绍</h3>
<ul>
<li>数据存储在用户浏览器中</li>
<li>设置、读取方便、甚至页面刷新不丢失数据</li>
<li>容量较大,<code>sessionStorage</code> 和 <code>localStorage</code> 约 5M 左右</li>
</ul>
<h3 id="本地存储分类">本地存储分类</h3>
<h4 id="localstorage">localStorage</h4>
<p>可以将数据<code>永久存储在本地</code>(用户的电脑), 除非<code>手动删除</code>,否则<code>关闭页面也会存在</code></p>
<p>特性:</p>
<ul>
<li>可以多窗口(页面)</li>
<li>共享(同一浏览器可以共享)</li>
</ul>
<p>语法:</p>
<ul>
<li>存储/修改数据:<code>localSotrage.setItem('key', 'value')</code></li>
<li>获取数据:<code>localStorage.getItem('key')</code></li>
<li>删除数据:<code>localStorage.removeItem('key')</code></li>
<li>清空数据:<code>localStorage.clear()</code></li>
</ul>
<h4 id="sessonstorage">sessonStorage</h4>
<p>特性:</p>
<ul>
<li>生命周期为关闭浏览器窗口</li>
<li>在同一个窗口(页面)下数据可以共享</li>
<li>以键值对的形式存储使用</li>
<li>用法跟 localStorage 基本相同</li>
</ul>
<h3 id="存储复杂数据类型">存储复杂数据类型</h3>
<p>本地只能存储字符串,无法存储<code>复杂数据类型</code>.</p>
<p>需要将复杂数据类型转换成<code>JSON字符串</code>,在存储到本地</p>
<p>语法:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"> <span class="nx">name</span><span class="o">:</span> <span class="s1">&#39;cengdong&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl"> <span class="nx">age</span><span class="o">:</span> <span class="mi">18</span>
</span></span><span class="line"><span class="cl"> <span class="nx">gender</span><span class="o">:</span> <span class="s1">&#39;男&#39;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 通过JSON.stringify序列化对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">localStorage</span><span class="p">.</span><span class="nx">setItem</span><span class="p">(</span><span class="s1">&#39;obj&#39;</span><span class="p">,</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">str</span> <span class="o">=</span> <span class="nx">localStorage</span><span class="p">.</span><span class="nx">getItem</span><span class="p">(</span><span class="s1">&#39;obj&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 通过JSON.parse反序列化字符串为对象
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">str</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div></description>
</item>
<item>
<title>【JavaScript】webAPIs四</title>
<link>https://www.cengdong.store/p/javascriptwebapis%E5%9B%9B/</link>
<pubDate>Tue, 30 Apr 2024 04:12:05 +0800</pubDate>
<guid>https://www.cengdong.store/p/javascriptwebapis%E5%9B%9B/</guid>
<description><h2 id="日期对象">日期对象</h2>
<h3 id="实例化">实例化</h3>
<p>时间对象实例化的两种方式:</p>
<ul>
<li>获得当前时间:<code>const date = new Date()</code></li>
<li>获得指定时间:<code>const date = new Date('2008-08-08')</code></li>
</ul>
<h3 id="时间对象方法">时间对象方法</h3>
<table>
<thead>
<tr>
<th>方法</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>getFullYear()</td>
<td>获得年份</td>
<td>获取四位年份</td>
</tr>
<tr>
<td>getMonth()</td>
<td>获得月份</td>
<td>取值为 0 ~ 11</td>
</tr>
<tr>
<td>getDate()</td>
<td>获取月份中的每一天</td>
<td>不同月份取值也不相同</td>
</tr>
<tr>
<td>getDay()</td>
<td>获取星期</td>
<td>取值为 0 ~ 6</td>
</tr>
<tr>
<td>getHours()</td>
<td>获取小时</td>
<td>取值为 0 ~ 23</td>
</tr>
<tr>
<td>getMinutes()</td>
<td>获取分钟</td>
<td>取值为 0 ~ 59</td>
</tr>
<tr>
<td>getSeconds()</td>
<td>获取秒</td>
<td>取值为 0 ~ 59</td>
</tr>
</tbody>
</table>
<h3 id="时间戳">时间戳</h3>
<p>获得时间戳的三种方式:</p>
<ul>
<li>对象方法:<code>date.getTime()</code></li>
<li>隐式转换:<code>const timeStamp = +new Date()</code></li>
<li>无需实例化:<code>Date.now()</code></li>
</ul>
<h2 id="节点操作">节点操作</h2>
<h3 id="dom-节点">DOM 节点</h3>
<p>有三种节点类型:</p>
<ul>
<li>元素节点</li>
<li>属性节点</li>
<li>文本节点</li>
</ul>
<h3 id="查找节点">查找节点</h3>
<h4 id="父节点">父节点</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">// 获取子节点
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">son</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">&#39;.dad&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 根据子节点获取父节点
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">son</span><span class="p">.</span><span class="nx">parentNode</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="子节点">子节点</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1