2017-09-17 1 views
0
내가 동형 앱을 반응 짓고 있어요

을에 초기 상태 (Initial)를 반환하고 다음 작업이 수행된다 그것을 반환합니다.
2) aboutPageContent() 액션을 전달하는 fetchData라는 정적 함수가 호출됩니다. 3) 서버에서 약속이 해결되면 업데이트 상태 인 을 얻기 위해 getState가 호출되지만 새로 업데이트 된 상태 인이 아닌 aboutPage의 initialState가 반환됩니다.돌아 오는 getState와 서버

감속기가 응답하지만 새 상태를 반환하지 않습니다. (이는 서버에 의해 작업이 전달되는 경우에만 발생합니다). 서버가 반환에

의 getState는 :

{ 
    aboutUs:{ 
     founders:[{}] 
     story: "" 
    } 
} 

그러나 반환해야합니다 :

{ 
    aboutUs:{ 
    founders:[{name: 'abc'}] 
    story: "some random story" 
    } 
} 

server.js

app.get("*", (req, res) => { 

const memoryHistory = createMemoryHistory(req.url) 
const currentRoute = routes.find(r => matchPath(req.url, r)) 
const store = configureStore({}, memoryHistory) 

if (currentRoute && currentRoute.component.fetchData) { 

    Promise.all(currentRoute.component.fetchData(store, req.url)).then(() => { 

     const content = (
      <StaticRouter location={req.url} context={{}}> 
       <Provider store={store}> 
        <App /> 
       </Provider> 
      </StaticRouter> 
     ) 

     const htmlContent = renderToString(content) 

     const preloadedState = store.getState() <----- Returns initialState and not the updated state. 

     const html = renderToStaticMarkup(<HtmlDocument html={htmlContent} preloadedState={preloadedState} />) 

     res.status(200) 
     res.send(html) 
    }) 
} 
}) 

aboutPageContainer.

static fetchData = (store) => [store.dispatch(aboutPageContent())] 

jsx

은 preloadedState는 window.__data에 할당됩니다. 그리고 클라이언트 저장소를로드하기 위해 window.__data이 client.js에서 호출됩니다.

뭐가 잘못 되었나요? 그것은 내 첫 반응 동형 앱입니다.

답변

1

아래의 업데이트를 받으려면 여기에 가입해야합니다. 을 사용하면 store.subscribe을 사용하면 업데이트 된 상태를 확인할 수 있습니다. 희망이 도움이됩니다. 읽기 http://redux.js.org/docs/api/Store.html#subscribelistener

store.subscribe(() => { 
// When state will be updated(in this case, when items will be fetched), this is how we can get updated state.      
    let items= store.getState().items; 
       })