We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
解析:Daily-Interview-Question/issues/1
The text was updated successfully, but these errors were encountered:
不加 key 再渲染时会更快。
验证,渲染 10_000 个<li>标签,在componentWillUpdate标记 render 开始时间,componentDidUpdate生命周期里打印时间差。
<li>
componentWillUpdate
componentDidUpdate
总共打印两次结果,第一次componentDidMount时更新 state,第二次间隔 1 秒更新 state 再打印。
componentDidMount
import React, { Component } from 'react'; import uuid from 'uuid/v1'; let time = 0 class App extends Component { state = { list: [] } componentDidMount() { const list = Array.from({length: 1000}).fill(null).map(() => ({ value: uuid(), key: uuid() })); this.setState({list}) setTimeout( () => { const list = Array.from({length: 1000}).fill(null).map(() => ({ value: uuid(), key: uuid() }), 1000); this.setState({list}) }) } componentWillUpdate() { time = +Date.now() } componentDidUpdate() { console.log(+Date.now() - time) } render() { const {list} = this.state; return ( <div className="App"> <ul> {list.map((item,idx) => ( <li>{item.value}</li> // <li key={item.key}>{item.value}</li> ))} </ul> </div> ); } }
不加 key 的结果:
加 key 的结果:
Sorry, something went wrong.
同时满足以下条件时可以使用数组索引作为 key 值:
No branches or pull requests
解析:Daily-Interview-Question/issues/1
The text was updated successfully, but these errors were encountered: