React-Router:链接更新URL但视图不会更改

时间:2018-01-31 13:54:44

标签: javascript reactjs redux react-router react-redux

我尝试了多种解决方案,但似乎没有工作,所以这里是代码:

预期行为:

  • 点击新的Deck按钮

  • 输入新的甲板名称并按Enter键,添加新甲板

  • 点击新甲板,重定向到网址中的/ deck /:deckId,其中deckId是套牌的ID

  • VisibleCards组件触发并返回包含当前deckId的div

实际行为:

  • 点击新的Deck按钮

  • 输入新的甲板名称并按Enter键,添加新甲板

  • 点击新甲板,重定向到网址中的/ deck /:deckId,其中 deckId是套牌的id

  • VisibleCards不会触发,如果我手动刷新页面就会触发

*另请注意,如果我手动输入网址,VisibleCards组件会触发*

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:

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

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

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));

1 个答案:

答案 0 :(得分:0)

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