鍍金池/ 問答/HTML5  HTML/ 關(guān)于react-router v4 官方示例中 Link 中 to 作為對象里的

關(guān)于react-router v4 官方示例中 Link 中 to 作為對象里的 state

最近在看 react-router v4api ,在 https://reacttraining.com/react-router/web/api/Link/to-object 中看到一段代碼,

<Link to={{
  pathname: '/courses',
  search: '?sort=name',
  hash: '#the-hash',
  state: { fromDashboard: true }
}}/>

這里的state: { fromDashboard: true },我完全沒看懂,
百度了很多,沒有找到,
剛開始以為是點擊這個 Link 時會改變 state 做了嘗試,發(fā)現(xiàn)并不是,
我還看了 Link 的源碼,也沒有發(fā)現(xiàn)什么奧秘,
所以由于好奇心的驅(qū)使,我提出了這個問題,
想知道這個state到底是什么意思,或者是舉例有錯誤?
求解答。

回答
編輯回答
網(wǎng)妓

state的內(nèi)容都是自定義的。你可以在Link的pathname對應(yīng)的Route的render或者children里面中的location對象拿到你自定義的state,然后做什么完全取決你

2018年4月14日 06:08
編輯回答
耍太極

傳參用的吧,去看下location

2017年12月27日 14:32
編輯回答
空白格

原生的history.pushState()方法可以傳入一個 state,有些瀏覽器會把它保存起來,重啟瀏覽器的時候恢復(fù) state:

state object — The state object is a JavaScript object which is associated with the new history entry created by pushState(). Whenever the user navigates to the new state, a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

The state object can be anything that can be serialized. Because Firefox saves state objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage.

https://developer.mozilla.org/en-US/docs/Web/API/History_API

2017年8月3日 03:14