我已经搜索了无济于事,Google上的所有链接都是紫色的。
我有一个基本的preact设置,我正在尝试使服务器端呈现正常工作,但是无论我怎样尝试,我都会不断收到此错误:
Warning: Failed prop type: Invalid prop `children` supplied to `Router`, expected a ReactNode.
in Router
Express服务器:
app.get('/', (req, res) => {
const App = () => (
<StaticRouter location={req.url} context={{}}>
<AppComponent />
</StaticRouter>
);
console.log(render(<App />)); // <undefined></undefined>
res.send('hello');
});
AppComponent:
@withRouter
export class AppComponent extends Component {
render() {
return (
<Switch>
<Route exact path="/" component={Home} />
</Switch>
);
}
}
Webpack配置:
module.exports = {
entry: './server.tsx',
output: {
path: join(__dirname, 'dist'),
filename: 'server.js'
},
target: 'node',
externals: [externals()],
resolve: {
extensions: ['.ts', '.tsx', '.js'],
alias: {
react: 'preact-compat',
'react-dom': 'preact-compat',
'create-react-class': 'preact-compat/lib/create-react-class'
}
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'ts-loader'
}
]
}
};
我可以使用SSR来获取内容,而不能使用StaticRouter。它总是抛出此错误。我尝试使用子元素进行嵌套,而不是嵌套,删除路线等。
版本:
react-router v5.0
react-router-dom v5.0
preact v8.4.2
preact-compat v3.18.5
preact-render-to-string v4.1.0