What is a state in <Link> component of React Router?

后端 未结 4 2110
遥遥无期
遥遥无期 2021-01-04 12:11

Here is a screenshot from their docs about component

  1. What state do they mean? A Redux state?
4条回答
  •  佛祖请我去吃肉
    2021-01-04 12:31

    It's a piece of information that you'd like to send to the next page. Nothing to do with Redux. It's a plain object. I believe Flipkart is a very nice example of how it can be used to improve user experience:

    • Go to a Flipkart search page on a mobile device (or simulate one using Chrome DevTools)
    • Tap on one of the items

    You'll see that the transition happens instantly and pieces of information like product images, title, rating and price are readily available on the product page. One way to implement that is passing the state they had already loaded on the search page onto the next one:

    
    

    And then:

    function ProductPage(props) {
      // Always check because state is empty on first visit
      if (props.location.state.product) {
        console.log(props.location.state.product);
        // { id: '...', images: [...], price: { ... } }
      }
    }
    

提交回复
热议问题