我第一次使用React和Redux进行服务器端渲染,看起来有点困难。我收到了警告:
Warning: Did not expect server HTML to contain a <li> in <ul>.
我看了这个,这意味着有一个html树不匹配。我不确定那是怎么回事。有没有明显的方法来解决它?这是我发出警告的代码。
import React, { Component } from 'react';
import { connect } from 'react-redux';
import actions from '../actions';
class UsersList extends Component {
componentDidMount() {
if (this.props.users.length > 0) {
return;
}
this.props.fetchUsers();
}
render() {
const { users } = this.props;
return (
<div>
<ul>
{users.map(user => {
return <li key={user.id}>{user.name}</li>;
})}
</ul>
</div>
);
}
}
const stateToProps = state => {
return {
users: state.users
};
};
const dispatchToProps = dispatch => {
return {
fetchUsers: () => dispatch(actions.fetchUsers())
};
};
const loadData = store => {
return store.dispatch(actions.fetchUsers());
};
export { loadData };
export default connect(stateToProps, dispatchToProps)(UsersList);
这是client.js:
// Startup point for the client side application
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import Routes from './Routes';
import reducers from './reducers';
const store = createStore(reducers, {}, applyMiddleware(thunk));
ReactDOM.hydrate(
<Provider store={store}>
<BrowserRouter>
<div>{renderRoutes(Routes)}</div>
</BrowserRouter>
</Provider>,
document.querySelector('#root')
);
答案 0 :(得分:4)
问题在于,由于服务器已经呈现了用户列表,因此它具有ul
li
秒。但是当客户端加载时没有初始数据,因此只有ul
并且没有li
。
要解决这个问题,我认为这个问题不多,因为无论如何在服务器端渲染中需要这样做是为了将一些初始状态传递给createStore
redux函数:
您需要在两个地方执行此操作。在服务器端的html和客户端的入口点。
简短的例子可能是:
<html>
<head></head>
<body>
<div id="root">${content}</div>
<script>
window.INITIAL_STATE = ${serialize(store.getState())}
</script>
<script src="bundle.js"></script>
</body>
</html>
如您所见,我们必须将初始状态设置为您在服务器端创建的商店。
在您的客户端:
// Startup point for the client side application
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import Routes from './Routes';
import reducers from './reducers';
const store = createStore(reducers, window.INITIAL_STATE, applyMiddleware(thunk));
ReactDOM.hydrate(
<Provider store={store}>
<BrowserRouter>
<div>{renderRoutes(Routes)}</div>
</BrowserRouter>
</Provider>,
document.querySelector('#root')
);
现在您可以访问此全局变量window.INITIAL_STATE
,然后将其传递给createStore
的第二个参数,即初始状态。
答案 1 :(得分:0)
我现在在这里发布,因为我正在搜索nextJS SSG的确切问题。就像戴夫·牛顿(Dave Newton)所述,在jsx返回中,如果由于对<ul>
进行了条件检查,则只添加了一个检查并且不呈现<li>
为空。像这样:
{Object.keys(el[i]).length > 0 ?
<ul>
{getKeys(docs, item, el).map(
([key, value], i) => {
const name = `${docs[item][el][key].name}`
const path = `${docs[item][el][key].path}`
return (name !== "undefined") && (
<li key={i}><a href={path}>{name}</a></li>
)}
)}
</ul>
: ''
}