用react实现v-model
叶江怀 2019-05-11
# React 性能优化
- 按需render整个页面
class Test extends Component {
constructor(props) {
super(props);
this.state = { arr: ["苹果", "香蕉"], value: "" };
}
//当值完全改变了以后才允许更新数据,而不是在onchange实时监听的情况下,实时更新render
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.arr !== this.state.arr) return true;
return false;
}
render() {
return (
<div>
<input type="text" value={this.state.value} />
<button onClick={() => this.btnHandle()}>添加</button>
{this.state.arr.map((val, idx) => {
return <div>{{ val }}</div>;
})}
</div>
);
}
btnHandle() {
let newArr = this.state.arr;
this.setState({
arr: newArr.push(this.state.value),
});
}
}
export default Test;
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
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