You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReact,{Component}from'react';import{BrowserRouterasRouter,Route,Switch,Link,withRouter,Redirect}from'react-router-dom';constHome=()=><h1>home</h1>;constAbout=(props)=>{console.log(props);return(<h1>about</h1>)};constUser=()=><h1>user</h1>;constLogin=()=><h1>login</h1>;constAddressBtn=withRouter(classAddressBtnextendsComponent{constructor(props){super(props);}prevPage=()=>{let{ history }=this.props;history.goBack();}nextPage=()=>{let{ history }=this.props;history.goForward();}render(){return(<div><buttononClick={this.prevPage}>上一页</button><buttononClick={this.nextPage}>下一页</button></div>)}})// 当我们设置replace属性为true时,会替换当前历史访问记录中的地址,而不是添加一个新的地址到历史访问记录中classAppextendsComponent{render(){return(<Router><div><AddressBtn/><Linkto="/">home</Link><Linkto="/about"replace>about</Link><Linkto="/user">user</Link><Linkto="/login">login</Link><Routepath="/"exactcomponent={Home}/><Routepath="/about"component={About}/><Routepath="/user"component={User}/><Routepath="/login"component={Login}/></div></Router>)}}exportdefaultApp;
react路由组件之Link组件
在react应用中,react路由组件可以提供可声明的,可访问的导航组件。这个组件就是组件,组件最终会被渲染成a标签。
Link组件有以下几个属性
除了上面这些属性,我们也可以添加别的属性,id,className,title等,就像给a标签添加属性一样。
结果为:
结果为:
当我们在Link组件中设置replace属性为true时,点击该链接,会替换当前路由地址,而不是在历史访问记录里添加新的一个地址。当我们依次点击login,user,about按钮时,然后再点击上一页按钮,我们发现链接不会跳转到user,说明这里的user地址被about地址给替换了。
上面代码中,我们创建一个引用,然后当组件在加载的时候,会调用lookInnerRef函数,然后可以访问到底层的组件引用。
The text was updated successfully, but these errors were encountered: