React-Router的Link-To更新URL但不刷新页面

TCa*_*ySF 5 reactjs react-router

我网站的标题有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)
Run Code Online (Sandbox Code Playgroud)

headerMenuCard:

var HeaderMenuCard = React.createClass({

...
    return(
        <div >
            <Link to="categoryShow" params={{id: this.props.id}} ></Link>
        </div>
    )
})
Run Code Online (Sandbox Code Playgroud)

这是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>
    )
}
})
Run Code Online (Sandbox Code Playgroud)

kud*_*ajz 3

您将在 props 中收到新参数,因此您只需要运行或中fetchData的任何其他逻辑。componentWillReceivePropscomponentWillUpdate