reactjs – React-Router的Link-To更新URL但不刷新页面
发布时间:2020-12-15 20:31:15 所属栏目:百科 来源:网络整理
导读:我网站的标题有10个类别图像(链接).每个都使用React-Router的链接路由到每个类别的相应categoryShow. 该链接从categoryIndex开始工作,但在从cagetoryShow单击时不再有效.它在点击时正确更新浏览器,例如它将pushState推送到/ cateories / 18和/ categories /
我网站的标题有10个类别图像(链接).每个都使用React-Router的链接路由到每个类别的相应categoryShow.
该链接从categoryIndex开始工作,但在从cagetoryShow单击时不再有效.它在点击时正确更新浏览器,例如它将pushState推送到/ cateories / 18和/ categories / 2,但浏览器不刷新. 值得注意的是,链接适用于所有其他Index-type和Show-type页面.它只是在categoryShow中不起作用.我想知道是否连续点击相同的名称,例如Link to =“categoryShow”,以某种方式阻止路由器进行页面刷新?编辑:我尝试将其更改为链接到= {“/ categories /”this.props.id},它也做同样的事情. 这是值得注意的组件结构.通过更新URL,所有数据都被成功传递.只是在一个特定情况下页面不刷新: -categoryShow -header (fetches and passes category data to child) -headerMenu (receives category data,passes on to child) -headerMenuCard (receives category data,uses the id in the link seen below) headerMenuCard: var HeaderMenuCard = React.createClass({ ... return( <div > <Link to="categoryShow" params={{id: this.props.id}} ></Link> </div> ) }) 这是CategoryShow,链接路由到的位置: var CategoryShow = React.createClass({ getInitialState: function(){ return{ didFetchData: false,items: [],userID: localStorage.getItem('userID'),headerImage: "../categories.png" } },componentDidMount: function(){ this.fetchData() },fetchData: function(){ var data = { userID: this.state.userID } var params = this.props.params.id $.ajax({ type: "GET",url: "/categories/" + params,data: data,dataType: 'json',success: function(data){ this.setState({didFetchData: 'true',items: data.items}) }.bind(this),error: function(data){ alert("error! couldn't fetch category data") } }) },render: function(){ var itemArray = this.state.items.map(function(item){ return <ItemCard name={item.name} key={item.id} id={item.id} photo_url={item.photo_url} description={item.description} userID={localStorage.getItem('userID')} like_status={item.like_status} /> }) return( <div> <Header /> <section className="body-wrapper"> {itemArray} </section> </div> ) } }) 解决方法
您将在props中接收新参数,因此您只需要在componentWillReceiveProps或componentWillUpdate中运行fetchData或任何其他逻辑.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |