将URL与特定状态配对

时间:2017-08-03 21:07:24

标签: javascript reactjs react-router-v4

我正在使用react工作的网站,我有几个不同的页面,你可以导航到不刷新页面或任何东西,只是根据currentPage变量将对象移入和移出视图。

基本上我希望能够输入特定链接将我带到某个页面,即 http://my-website.com/about 会执行this.setState({currentPage:'about'})

我还希望在您更改网页时更改网址,如果您点击“购买”网址更改为 http://mywebsite.com/shop

如果这个问题有些模糊,我很抱歉,我不知道如何描述我想要完成的事情。如果有任何不清楚的地方,请随时问我。

1 个答案:

答案 0 :(得分:2)

使用React-Routerpublic extension NSRange { private init(string: String, lowerBound: String.Index, upperBound: String.Index) { let utf16 = string.utf16 let lowerBound = lowerBound.samePosition(in: utf16) let location = utf16.distance(from: utf16.startIndex, to: lowerBound) let length = utf16.distance(from: lowerBound, to: upperBound.samePosition(in: utf16)) self.init(location: location, length: length) } public init(range: Range<String.Index>, in string: String) { self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound) } public init(range: ClosedRange<String.Index>, in string: String) { self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound) } } 使用History Web API观看有效网址,并根据有效网址更改您的观看次数。

相关问题