react router native : rn to web使用link標籤失效

rn項目的路由使用link標籤可以成功跳轉

const linkParams = {
    pathname: '/star',
    state: { data: item },
}

<Link to={ linkParams } component={ TouchableOpacity }>
    <Item text={ item.text } index={ index }/>
</Link>

但是在rn to web時,這種寫法的link會失效不跳轉。因此使用history.push代替<Link>標籤,時rn和web環境時都可以跳轉

const linkParams = {
    pathname: '/star',
    state: { data: item },
}

<TouchableOpacity onPress={ ()=>{ this.props.history.push(linkParams) } }>
    <Item text={ item.text } index={ index }/>
</TouchableOpacity>

history.push的跳轉原理和<Link>是一樣的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章