Your console
should have already warning about one thing: every child in an element must have a unique key. In your code:
{this.props.elements.map((element, i) =>{
return <div>
<input
onChange={(event) => {this.inputChange(i, event.target.value)}}
value={element.value} key={i} />
</div>
})}
Should be:
{this.props.elements.map((element, i) => (
<div key={i}>
<input
onChange={(event) => {this.inputChange(i, event.target.value)}}
value={element.value} key={i} />
</div>
)}
Note: key
is an internal param used by React. Don't use it as a normal prop.
That will optimize a bit, but you are treating with a huge array and rendering it full. This is a lot of job done in handleInputChange
and behind the scenes during render by React.
You can use some kind of onScroll
event handler to render the data as it is shown. There are several options. Here a tutorial using InifiniteLoader.
Besides that, you'll need to optimize how data is saved. Modifying/recreating a thousands elements array (and an object!) on each key press does not sounds good. Either saving a different state for each element, or having a optimized setter.
Another kind of optimizations that may help is to avoid declaring functions inside the render. For example, on component mount, you can create an array of setters:
componentDidMount() {
const inputHandlers = this.state.elements
.map( (el, i) => this.handleInputChange.bind(this, i));
// Or:
// .map( (el, i) => (event) => this.handleInputChange(i, event.target.value));
this.setState({inputHandlers});
}
//...
<ChildComponent
elements={this.state.elements}
handleInputChange={this.inputHandlers}
/>
Then, in the child component:
<div>
<input
onChange={ this.state.inputChange[i] }
value={element.value}/>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…