简体   繁体   English

localStorage-Spotify访问令牌刷新后保存输入值

[英]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: 例如,以下是步骤:

  1. Learner searches a term 学习者搜索词
  2. The Spotify API goes fetches the access token and refreshes the page Spotify API去获取访问令牌并刷新页面

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值,不如使用内置的getset方法。

localStorage.setItem(key, value)
localStorage.getItem(key)

Here's an example from MDN. 这是来自MDN的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM