react-router in the history

react-router in the history

eact-router  is built on the history of

a history management application session session js js library history. It different environments (browsers, node ...) unified variables into a simple API to manage history stack, navigation, confirmation jump, and continued state between sessions.



  
  
  1. // Basic use
  2. import { createHistory } from 'history'
  3. const history = createHistory()
  4. // current address
  5. const location = history.getCurrentLocation()
  6. // Listen for the current address translation
  7. const unlisten = history.listen( location => {
  8. console.log(location.pathname)
  9. })
  10. // The new entrance into the history stack
  11. history.push({
  12. pathname: '/the/path',
  13. search: '?a=query',
  14. Some // url parameter does not exist above the value of the current state of the url
  15. state: { the: 'state' }
  16. })
  17. // When you're finished, stop the listener
  18. unlisten()

You can also object to the use of history to change the current location:

  • push(location)
  • replace(location)
  • go(n)
  • goBack()
  • goForward()

A history knows how to monitor changes in the browser address bar, and parse the URL into the location object, and then match it to the router using the routing, the last correctly render the corresponding components.

location objects include:


   
   
  1. pathname 同window.location.pathname
  2. search the same window.location.search
  3. state a bundle object object on this address
  4. action PUSH, REPLACE, one or POP
  5. key unique ID
Three common history

    
    
  1. // HTML5 history, recommended
  2. import createHistory from 'history/lib/createBrowserHistory'
  3. // Hash history
  4. import createHistory from 'history/lib/createHashHistory'
  5. // memory history (eg: node environment)
  6. import createHistory from 'history/lib/createMemoryHistory'

createHashHistory

This is one you will get the default history, if you do not specify a history (ie <Router> {/ * your routes * /} </ Router>). It is used in the URL hash (#) to create shaped like http://example.com/#/some/path part of the route.

Hash history is the default because it can not make any configuration can be run on the server, and it includes IE8 + can be used in all popular browsers. But we do not recommend to use it in actual production, because every web application should have a destination to use createBrowserHistory.

createBrowserHistory

Browser history is to create a browser application recommended by the history React Router. It uses  History API was created to handle URL in the browser, such as a new real `URL example.com / some / path`

createMemoryHistory

Memory history will not be operated or read in the address bar. This explains how we achieve server rendering. At the same time it is also very suitable for testing and other rendering environment (like React Native).

For details, please see Address: https://zhuanlan.zhihu.com/p/20799258?refer=jscss


                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">1</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/jimolangyaleng">
                    <img src="https://profile.csdnimg.cn/5/C/7/3_jimolangyaleng" class="avatar_pic" username="jimolangyaleng">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/1x/8.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/jimolangyaleng" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">狼牙冷</a></span>
                                            </div>
                    <div class="text"><span>发布了46 篇原创文章</span> · <span>获赞 28</span> · <span>访问量 25万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=jimolangyaleng" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
Published 10 original articles · won praise 0 · Views 323

Guess you like

Origin blog.csdn.net/qq_30627241/article/details/103802454