【问题标题】:get props object from link react-router-native从链接 react-router-native 获取 props 对象
【发布时间】:2018-05-28 12:05:36
【问题描述】:

如何做到这一点?例如我发现这个:

<Link to={{pathname: "/dialog", state: { detail: message }}}>
text
</Link>

但如果我想显示 this.props.location.state,则写 undefined

【问题讨论】:

    标签: react-native react-router-native


    【解决方案1】:

    传递参数

    Configure your route path to accept named parameters (:id):
    
    <Route path='/route/:id' exact component={MyComponent} />
    Then you can pass URL parameters such as IDs in your link to:
    
    <Link to={`route/foo`}>My route</Link>
    You can access the parameter within your component via the match object:
    
    const {id} = props.match.params
    
    console.log(id) // "foo"
    

    【讨论】:

    • &lt;Route path="/dialog/:id" component={() =&gt; &lt;Dialog showMenuIcon={this.showMenuIcon} /&gt;} /&gt;
    • &lt;Link to={"/dialog/" + message}&gt;,消息是对象,但连字符串都没有传递参数。
    猜你喜欢
    • 2022-01-17
    • 2020-11-05
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    相关资源
    最近更新 更多