我遇到了从本地存储中的数组获取数据的组件的问题。它在页面加载时获取初始数据,但是如何在 localstorage 更改时更新?
import React, {Component} from 'react';
class MovieList extends Component {
constructor(props){
super(props)
this.state = {
filmList: []
}
}
componentWillMount(){
var film = [],
keys = Object.keys(localStorage),
i = keys.length;
while ( i-- ) {
film.push( localStorage.getItem(keys[i]))
}
this.setState({filmList: film})
};
render(){
return(
<ul>
<li>{this.state.filmlist}</li>
</ul>
);
}
}
export default MovieList;