简体   繁体   English

React-Router:链接更新URL,但视图不变

[英]React-Router: Link updates URL but view does not change

I've tried multiple solutions but none seem to work so here's the code: 我尝试了多种解决方案,但似乎都没有用,所以这里是代码:

Expected Behavior: 预期行为:

  • Click on new Deck button 单击新的甲板按钮

  • Enter new Deck name and press enter, new deck is added 输入新的牌组名称,然后按Enter,添加新的牌组

  • Click on new Deck,get redirected to /deck/:deckId in the url, Where deckId is the id of the deck 单击新的Deck,将其重定向到URL中的/ deck /:deckId,其中deckId是Deck的ID

  • VisibleCards component fires and returns a div containing current deckId VisibleCards组件触发并返回一个包含当前deckId的div

Actual Behavior: 实际行为:

  • Click on new Deck button 单击新的甲板按钮

  • Enter new Deck name and press enter, new deck is added 输入新的牌组名称,然后按Enter,添加新的牌组

  • Click on new Deck,get redirected to /deck/:deckId in the url, Where deckId is the id of the deck 单击新的Deck,将其重定向到URL中的/ deck /:deckId,其中deckId是Deck的ID

  • VisibleCards does not fire, it fires if i refresh the page manually VisibleCards不会触发,如果我手动刷新页面会触发

*Also please note that if i enter the url manually, the VisibleCards component fires * *另请注意,如果我手动输入网址,VisibleCards组件会触发*

app.js: app.js:

//STORE
const store = createStore(combineReducers(reducers));
const history = syncHistoryWithStore(createBrowserHistory(), store);

function run(){
    let state = store.getState();
    console.log(state);
    ReactDOM.render(
        <Provider store={store}>
            <Router history={history}>
                <div className="holder">


                    <div className="sidebar">
                        <Route path='/' component={App}/>
                    </div>
                    <div className="VisibleCards">
                        <Route path='/deck/:deckId' component={VisibleCards}/>
                    </div>
                </div>


            </Router>       
        </Provider>,document.getElementById('root'));
}
run();
store.subscribe(run)

window.show = ()=>{store.dispatch(showAddDeck())};
window.hide = ()=>{store.dispatch(hideAddDeck())};
window.add = ()=>{store.dispatch(addDeck(new Date().toString()))};

VisibleCards.js: VisibleCards.js:

function mapStateToProps(state, ownProps) {
  //console.log(ownProps.location.pathname);
  console.log(`currently is visible cards.js , ownprops is: `);
  console.log(ownProps)
  return {state};
}

class Cards extends Component{
    constructor(props){
        super(props)
        //console.log("Currently in VisibleCards, props are:")
        //console.log(props)
    }
    render(){
        return (
            <div>{this.props.match.params.deckId}</div>
        )
    }
}
export default withRouter(connect(mapStateToProps)(Cards));

App.js App.js

function mapStateToProps(state, ownProps) {
  //console.log(ownProps.location.pathname);
  console.log(`currently is App.js , state is: `);
  console.log(state);
  console.log(`currently is App.js , ownProps is: `)

  //var deckId = (ownProps.location.pathname).substring(6);
  console.log(ownProps)
  //console.log(state);
  return {state};
}

class App extends Component{
    constructor(props){
        super(props);
    }
    componentWillReceiveProps(nextProps) {
      if (nextProps.location !== this.props.location) {
        // navigated!
        console.log("here");
      }
      else{
        console.log("In app.js else statment,this.props are:")
        console.log(this.props);
        //console.log("nextprops are:")
        //console.log(nextprops);
      }
    }
    render(){
        return (
            <div className="app">
                <h1>{this.props.deckId}</h1>
                <Sidebar/>
                {this.props.children}
            </div>
        )
    }
}

export default withRouter(connect(mapStateToProps)(App));

Sidebar.js Sidebar.js

class Sidebar extends Component{

    constructor(props){
        super(props)
        this.createDeck = this.createDeck.bind(this);
    }

    componentDidUpdate(){
        var el = ReactDOM.findDOMNode(this.refs.add)
        if(el){el.focus();}
    }
    createDeck(e){
        if(e.which!==13)return
        //console.log("here");
        console.log(this);
        var name = ReactDOM.findDOMNode(this.refs.add).value;
        //console.log("here");
        this.props.addDeck(name);
        this.props.hideAddDeck();
    }


    render(){
        let props = this.props
        //console.log(props)
        return (
            <div className="sidebar">
                <h2>All decks</h2>
                <button onClick = {e=>this.props.showAddDeck()  }>
                    New Deck
                </button>
                <ul>
                    {props.decks.map((deck,i)=>
                        <li key={i}>
                            <Link  to={`/deck/${deck.id}`} >{deck.name}</Link>
                        </li>
                    )}
                </ul>

                {props.addingDeck && <input ref='add' onKeyPress = {this.createDeck}></input>}
            </div>)
    }
}
const mapStateToProps = ({decks,addingDeck})=>({
    decks,
    addingDeck
});

const mapDispatchToProps = (dispatch)=>({
    addDeck : name => dispatch(addDeck(name)),
    showAddDeck : () => dispatch(showAddDeck()),
    hideAddDeck : () => dispatch(hideAddDeck())
})



export default withRouter(connect(mapStateToProps,mapDispatchToProps)(Sidebar));

我猜组件正在触发componentWillUpdate(),如果您从同一路径执行操作,则组件可能正在接收新的道具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM