Skip to content

Commit fdefd41

Browse files
committed
edit md
1 parent 9a8ea6c commit fdefd41

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎react/component/src/state/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,43 @@ ReactDOM.render(
7777
)
7878
```
7979

80+
# setState
81+
82+
React 的语法是`{}`,单向数据绑定,Vue 的语法是`{{}}`,双向数据绑定
83+
84+
Vue 里面
85+
86+
1. data变了,view层通过`v-xxx`或者`{{}}`指令,来渲染数据 M->V
87+
88+
2. view变了,你通过`v-on`或者`v-model`来把数据从视图层带回数据层 V-M
89+
90+
React 里面
91+
92+
1. State变了,是通过函数式编程和`{}`,来渲染数据 S->V
93+
94+
2. view变了,通过事件触发`setState`来更改数据层 V->S
95+
96+
所以我们要更新视图,就必须把关注点放在更改 state 上,而更改 state 就要通过`setState`方法来实现
97+
98+
```html
99+
<!-- html -->
100+
<button onClick={this.changeValue.bind(this)}>{this.state.title</button>
101+
```
102+
上面的`<button>`值会因为点击而改变,原因就是因为用户触发了点击事件,而事件动用了`setState`方法更改`state`的状态,从而更新按钮的值
103+
```js
104+
changeValue(){
105+
console.log(this);
106+
this.setState({
107+
"title":"支付宝"
108+
})
109+
}
110+
```
111+
80112
### 效果预览
81113

82114
- [ES5中的state](https://wscats.github.io/react-tutorial/react/component/src/state/es5-state.html)
83115
- [ES6中的state](https://wscats.github.io/react-tutorial/react/component/src/state/es6-state.html)
116+
- [setState的用法](https://wscats.github.io/react-tutorial/react/component/src/state/setState.html)
84117

85118
# 思考
86119

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<div id="demo">
13+
14+
</div>
15+
<script src="../js/react.js"></script>
16+
<script src="../js/react-dom.js"></script>
17+
<script src="../js/babel.js"></script>
18+
<!-- ES6 JSX -->
19+
<script type="text/babel">
20+
class Xbtn extends React.Component {
21+
constructor(props){
22+
super(props)
23+
// 父传子 老爸给我的
24+
this.props = props;
25+
// 自己拥有的 就是vue组件里面data Model
26+
this.state = {
27+
"title":"微信"
28+
};
29+
}
30+
changeValue(){
31+
console.log(this);
32+
this.setState({
33+
"title":"支付宝"
34+
})
35+
}
36+
// view
37+
render() {
38+
return <button onClick={
39+
this.changeValue.bind(this)
40+
}>{this.state.title}</button>;
41+
}
42+
}
43+
ReactDOM.render(
44+
<div>
45+
<input type="password" />
46+
<Xbtn name="btn" />
47+
</div>,
48+
document.querySelector("#demo")
49+
)
50+
51+
</script>
52+
</body>
53+
54+
</html>

0 commit comments

Comments
 (0)