[英]localStorage - save input value after Spotify Access Token refresh
I'm making a ReactJS app and I'm trying to save the search input value after my Spotify API refreshes and gets the access token. 我正在制作一个ReactJS应用程序,并试图在我的Spotify API刷新并获取访问令牌之后保存搜索输入值。 For example, here are the steps:
例如,以下是步骤:
From step two, I would like the search term to be still inside the search input field (after the refresh). 从步骤二,我想搜索词是静静的搜索输入字段(刷新之后) 内 。
Here is my attempt: 这是我的尝试:
constructor(props){
super(props);
this.state = {
term: ''
};
...
document.getElementById('search-input') = window.localStorage['term'];
}
... ...
search(){
this.props.onSearch(this.state.term);
window.localStorage['term'] = this.state.term;
}
... ...
render(){
return (
<div>
<input id="search-input" onChange={this.handleTermChange} autoFocus/>
<a id="search" onClick={this.search}>SEARCH</a>
</div>
);
}
Thanks for the help! 谢谢您的帮助!
Rather than setting and getting your localStorage value with window.localStorage['term']
, you'll want to use the built-in get
and set
methods. 与其使用
window.localStorage['term']
设置和获取您的localStorage值,不如使用内置的get
和set
方法。
localStorage.setItem(key, value)
localStorage.getItem(key)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.