File tree Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 250250- [ React 组件 API] ( ./react/API.md )
251251- [ React 组件生命周期] ( ./react/组件生命周期.md )
252252- [ React Refs] ( ./react/Refs.md )
253+ - [ todoList总结] ( ./react/todoList.md )
253254
254255### 阶段二
255256
271272- [ Update和UpdateQueue] ( ./react/Update和UpdateQueue.md )
272273- [ react脚手架] ( ./react/react脚手架.md )
273274- [ 消息订阅与发布] ( ./react/消息订阅与发布.md )
275+ - [ fetch] ( ./react/fetch.md )
276+
274277
275278## 常见问题及解答
276279
Original file line number Diff line number Diff line change 1+ fetch的出现就是为了解决XHR的问题:
2+
3+ 使用XHR发送一个json请求一般是这样:
4+
5+ ``` js
6+ var xhr = new XMLHttpRequest ();
7+ xhr .open (' GET' , url);
8+ xhr .responseType = ' json' ;
9+
10+ xhr .onload = function () {
11+ console .log (xhr .response );
12+ };
13+
14+ xhr .onerror = function () {
15+ console .log (' eerror' )
16+ }
17+
18+ xhr .send ();
19+ ```
20+
21+ fetch:
22+
23+ ``` js
24+ fetch (' ' ).then (
25+ response => {
26+ console .log (' 联系服务器成功了' );
27+ return response .json ()
28+ },
29+ error => {console .log (' 联系服务器失败了' , error)}
30+ ).then (
31+ response => {console .log (' 获取数据成功了' , response)},
32+ error => {console .log (' 获取数据失败了' , error)}
33+ )
34+ ```
35+
36+ ``` js
37+ try {
38+ const response = await fetch (' ' )
39+ const data = await response .json ()
40+ console .log (data)
41+ } catch (error) {
42+ console .log (' 请求出错' , error)
43+ }
44+ ```
45+
Original file line number Diff line number Diff line change 1+ 1 . 拆分组件,实现静态组件,注意:className,style 的写法
2+ 2 . 动态初始化列表,如何确定将数据放在哪个组件的state中?
3+
4+ 某个组件使用:放在其自身的state中
5+
6+ 某些组件使用:放在他们共同的父组件state中
7+
8+ 3 . 关于父子之间通信
9+
10+ 【父组件】给【子组件】传递数据:通过props传递
11+
12+ 【子组件】给【父组件】传递数据:通过props传递,要求父提前给子传递一个函数
13+
14+ 4 . 注意defaultChecked 和 checked 的区别,类似的还有: defaultValue 和 value
15+
16+ 5 . 状态在哪里,操作状态的方法就在哪里
17+
18+ 设计状态时要考虑全面,例如带有网络请求的组件,要考虑请求失败怎么办。
19+
20+ ES6小知识点: 解构赋值+重命名
21+
22+ ``` js
23+ let obj = {a: {b: 1 } }
24+ const {a } = obj; // 传统解构赋值
25+ const {a: {b }} = obj; // 连续解构赋值
26+ const {a: {b: value }} = obj; // 连续解构赋值+重命名
27+ ```
28+
29+ 消息订阅与发布机制
30+
31+ 1 . 先订阅,再发布
32+ 2 . 适用于任意组件通信
33+ 3 . 要在组件的componentWillUnmount中取消订阅
34+
35+ fetch发送请求
36+
37+ ``` js
38+ try {
39+
40+ }catch (error) {
41+
42+ }
43+ ```
Original file line number Diff line number Diff line change 10102 . PubSub.subscribe('delete', function(data){}); // 订阅
11113 . PubSub.publish('delete', data) // 发布消息
1212
13+ ``` js
14+ var mySubscriber = function (msg , data ) {
15+ console .log (msg, data);
16+ };
17+
18+ var token = PubSub .subscribe (' MY TOPIC' , mySubscriber);
19+
20+ PubSub .unsubscribe (token);
21+ ```
22+
23+ ``` js
24+ var mySubscriber = function (msg , data ) {
25+ console .log ()
26+ }
27+
28+ PubSub .publish (' 消息名' , ' hello world' )
29+ ```
You can’t perform that action at this time.
0 commit comments