2016-02-18 25 views
4

加載本地存儲我有一個反應的應用程序等,其中我有App.js,其中包括我的HeadersFooters ..反應在第一

在那裏我存儲我的state到本地存儲..存儲我可以找回後。

但我在加載時遇到問題..在我的瀏覽器中,當我做localstorage['key']它給了我數據但我無法加載。

我應該在哪裏加載它?

我已經Root.js

class Root extends Component { 
    render() { 
    const { store } = this.props 
    return (
     <Provider store={store}> 
      <ReduxRouter /> 
     </Provider> 
    ) 
    } 
} 

Root.propTypes = { 
    store: PropTypes.object.isRequired 
} 

export default Root 

index.js在外級別:

const store = configureStore() 


render(

    <Root store={store} />, 
    document.getElementById('root') 
) 

我應該在哪裏加載我localStorage

需要幫助。我需要知道什麼是所謂的第一反應中,這樣我可以打個電話來加載localStorage有..如果不是壞主意

回答

0

我會建議從應用程序內的componentDidMount方法獲取本地存儲的數據。 js文件。

請找到的官方參考這裏: https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount

如果你想之前的組件被卸除,您可以使用該方法componentWillUnmount更新localStorage的數據:您使用的終極版 https://facebook.github.io/react/docs/component-specs.html#unmounting-componentwillunmount

+0

該問題說起初,所以'componentDidMount'沒有用。如果必須使用組件生命週期,那麼這是'componentWillMount',它比'componentDidMount'更早發生。 – AmerllicA

1

,所以你應作爲行動的結果讀取數據。

,使這項工作將是最簡單的方法:

  1. ReduxRouter應該有一個連接部件。請參閱docs
  2. 在該連接的組件中,添加componentDidMount以調用操作getStuffFromLocalstorage。見React lifecycle events
  3. 在你減速,讓你從localStorage的想要什麼,並填充狀態

這可能是一個反做你的減速,但它至少會做出了Redux流繞過去。

相關問題