简体   繁体   English

无法在 React 路由器链接中传递状态

[英]Can't pass state in React router link

I have a notification bubble that is supposed to link to a chat interface.我有一个应该链接到聊天界面的通知气泡。

I am trying to pass the state for the chat in the link that the bubble is in, but no luck.我试图通过气泡所在链接中的聊天状态,但没有运气。

class NotifBubble extends Component {

    componentWillMount() {
        this.pathname = "/chat/" + this.props.merchant.id
        if (this.props.merchant.id === 0) {
            this.pathname = '#';
        }

    }

    render() {
        if (typeof this.props.merchant === 'undefined') {
            return <div></div>
        }
        return (
            <Link className="notif-bubble-link" to={{
                pathname: this.pathname,
                state: { merchant: this.props.merchant }
            }}><div className="notif-bubble slide-in-right">
                    {this.props.message}
                </div></Link>
        );
    }
}

export default NotifBubble;

Everything is working correctly - up to the point where I pass the state, this.props.merchant, into the link.一切正常 - 直到我将状态 this.props.merchant 传递到链接中为止。 this.props.merchant is absolutely correct though. this.props.merchant 是绝对正确的。

What am I doing wrong here to not pass state correctly?我在这里做错了什么没有正确传递状态?

Never use this.pathname to store your component state. 切勿使用this.pathname来存储组件状态。 Use the React State API provided. 使用提供的React State API

Also it is preferred to initialize the state in the constructor (as opposed to componentWillMount ) 另外,最好在构造函数中初始化状态(与componentWillMount相对)

class NotifBubble extends Component {
  constructor(props) {
    super(props);

    const pathname = props.merchant.id === 0 ?
      '#' : `/chat/${props.merchant.id}`;

    this.state = {
      pathname
    };
  }

  render() {
    if (typeof this.props.merchant === 'undefined') {
      return <div > < /div>
    }
    return ( <
      Link className = "notif-bubble-link"
      to = {
        {
          pathname: this.state.pathname,
          state: {
            merchant: this.props.merchant
          }
        }
      } > < div className = "notif-bubble slide-in-right" > {
        this.props.message
      } <
      /div></Link >
    );
  }
}

export default NotifBubble;

To pass object, you need to stringify the object first, 要传递对象,您需要先对对象进行stringify处理,

<Link className="notif-bubble-link" 
    to={{pathname: this.pathname,
         state: { merchant: JSON.stringify(this.props.merchant)}
       }}>
   <div className="notif-bubble slide-in-right">
         {this.props.message}
   </div>
</Link>

To access state do this, 要访问state

const merchant = JSON.parse(this.props.location.state.merchant)

When using a pathname url that contains search parameters or hash parameters like this: /link?id=100 or /link#some-id then it won't pass the state object to the next route unless you move the parameters to their own designated properties like this:当使用包含这样的搜索参数或散列参数的路径名 url 时: /link?id=100/link#some-id那么它不会将状态对象传递到下一个路由,除非您将参数移动到它们自己指定的位置像这样的属性:

        <Link to={{
            pathname: '/link',
            search: '?id=100',
            hash: '#some-id',
            state: {
              id: 100,
            }
        }}>
          Click here
        </Link>

Hope that helps anyone who has a url with search or hash parameters.希望能帮助任何有搜索或哈希参数的 url 的人。 link to React-Router docs链接到 React-Router 文档

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

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