Closed EvanLiu2968 closed 6 years ago
sorry,忘记spa的ssr是手动引入的了,直接在layout传值就可以了<Layout {...context.state}>
完整代码应该是这样的
const serverRender = (context, options)=> {
const { ctx, request, helper, url} = context.state;
console.log("==================context")
// console.log(context)
const branch = matchRoutes(routes, url);
const promises = branch.map(({route}) => {
const fetch = route.component.fetch;
return fetch instanceof Function ? fetch(ctx) : Promise.resolve(null)
});
return Promise.all(promises).then(data => {
const initState = context.state;
data.forEach(item => {
Object.assign(initState, item);
});
context.state = Object.assign({}, context.state, initState);
const store = create(initState);
return () =>(
<Layout {...context.state}>
<Provider store={store}>
<StaticRouter basename="/ssrdemo" location={url} context={{}}>
<App url={url}/>
</StaticRouter>
</Provider>
</Layout>
)
});
};
现象:使用spa ssr渲染模式时,页面入口可以获取到服务端的ctx和初始数据,但在layout里面无法获取ctx和初始数据,正常多页面启用loader或者客户端渲染时是可以在layout获取到ctx的,没看出明白问题在哪?
预期:spa ssr渲染模式时layout也需要获取ctx和初始数据
version:egg-view-react-ssr@2.3.1
demo code spa ssr demo entry
layout.js