2016-06-01 5 views
9

React-Redux 생태계에 익숙하지 않아 간단한 애플리케이션을 사용해 보았습니다. 이 경우 나는 react-redux 어플리케이션에서 라우팅이 어떻게 작동 하는지를 시험 중이다. 는 기본적 아이디어는 다음과 같습니다 파견 비동기 작업을 성공적으로 완료되면 새 페이지로 연결되는 링크를 클릭하여 새 페이지React-Redux의 라우팅 문제

  1. 이동 (A 반응 라우터 구성 요소)
  2. 이동합니다.

여기에 내 코드

import React from 'react' 
    import {Link} from 'react-router' 
    import {routerActions} from 'react-router-redux' 
    import {connect} from 'react-redux' 

    class App extends React.Component { 
    render() { 
    // And you have access to the selected fields of the State too! 
    return (
     <div> 
      <header> 
       Links: 
       {' '} 
       <Link to="/">Home</Link> 
       {' '} 
       <Link to="/foo">Foo</Link> 
       {' '} 
       <Link to="/bar">Bar</Link> 
      </header> 
      <div> 
       <button onClick={() => routerActions.push('/foo')}>Go to /foo</button> 
      </div> 
     </div> 
     ) 
     } 
    } 
    export default connect(null, null)(App); 
    =================================================================== 
    import React from 'react' 
    import {connect} from 'react-redux' 
    class Foo extends React.Component { 
    render() { 
    return (
     <div> <h1>I'm Foo</h1> </div> 
     ) 
     } 
    } 
    export default connect(null, null)(Foo); 
    =================================================================== 
    import React from 'react' 
    import {connect} from 'react-redux' 
    class Bar extends React.Component { 
    render() { 
    return (
     <div> <h1>I'm bar</h1> </div> 
     ) 
     } 
    } 
    export default connect(null, null)(Bar); 

    =================================================================== 
    import React from 'react' 
    import ReactDOM from 'react-dom' 
    import {Provider} from 'react-redux' 
    import {Router, Route, browserHistory} from 'react-router' 
    import {syncHistoryWithStore} from 'react-router-redux' 
    import configureStore from './store' 
    import App from './components/test/App'; 
    import Bar from './components/test/Bar'; 
    import Foo from './components/test/Foo'; 
    // Get the store with integrated routing middleware. 
    const store = configureStore() 
    // Sync browser history with the store. 
    const history = syncHistoryWithStore(browserHistory, store) 
    // And use the prepared history in your Router 
    ReactDOM.render(
    <Provider store={store}> 
    <div> 
     <Router history={history}> 
      <Route path="/" component={App}> 
       <Route path="/foo" component={Foo}/> 
       <Route path="/bar" component={Bar}/> 
      </Route> 
     </Router> 
    </div> 
    </Provider>, 
    document.getElementById('root') 
    =================================================================== 

    import {combineReducers,createStore, applyMiddleware} from 'redux' 
    import thunk from 'redux-thunk' 
    import createLogger from 'redux-logger' 
    import userReducer from './reducers/reducer-user'; 
    import {routerMiddleware,routerReducer} from 'react-router-redux' 
    import {browserHistory} from 'react-router' 

    export default function configureStore() { 
    // Create the routing middleware applying it history 
    const browserMiddleware = routerMiddleware(browserHistory); 
    const logger = createLogger(); 
    const reducer = combineReducers({ 
    userState: userReducer, 
    routing: routerReducer 
    }) 
    const store = createStore(reducer,applyMiddleware(thunk,browserMiddleware,logger)); 
return store; 

}

응용 프로그램이 잘 구축 그리고 그것은 잘 온다하지만 난 링크를 클릭하면, 그것은 작동하지 않습니다.
screen shot of the running application
다양한 게시물을 검색하고 루트 문제를 정확히 찾아 낼 수 없습니다.

+0

나는 이것이 중복일지도 모른다 : http://stackoverflow.com/questions/35196873/how-to-use-react-router-redux-routeactions/37494808 - 본질적으로 당신이 필요로하는 것처럼 보입니다. 'this.props.dispatch (routeActions.push ('/ foo));' –

답변

1

코드가 정확하지만 보이지 않는 간단한 것이 있습니다. 라우터의 "하위"를 렌더링하지 않습니다! :)

당신은 여기에 체크 아웃 할 수 있습니다

https://github.com/reactjs/react-router-tutorial/tree/master/lessons/04-nested-routes#sharing-our-navigation

당신이 구성 요소 경로, 당신이 배치 될 위치를 지정해야합니다 (당신이 </Route path="application-path" component={MyComponent} /> 사용하여 선언 된)을 렌더링 할 때마다. 반응 라우터를 사용하여 children 소품을 사용하여 이것을 지정합니다. 그런 다음 React가이 소품을 "볼"때마다 경로를 렌더링합니다 (중첩 된 경로 일 수도 있음).

코드를 수정하려면 App 구성 요소가 this.props.children을 올바르게 처리해야합니다. 그런 뭔가 : 당신이 "/ foo는"경로를 쳤을 때

이제
class App extends React.Component { 
    /* ... */ 
    render() { 
     return (
      <div> 
       <header>Links go here</header> 
       {this.props.children} 
      </div> 
     ) 
    } 
} 

, this.props.childrenFoo 구성 요소에 의해 대체됩니다.

그런데 중첩 된 경로 (내부의 경로)는 "/"가 필요하지 않습니다. 그 이유는 경로가 "앞에"있기 때문입니다. 이것이 반응 라우터가 중첩 된 경로를 렌더링하는 방법입니다.

나는 그것이 다행이라고 생각합니다. :)