gpt4 book ai didi

javascript - React/Redux 服务器端渲染中的警告 : Did not expect server HTML to contain a
  • in
      .
  • 转载 作者:数据小太阳 更新时间:2023-10-29 05:21:20 26 4
    gpt4 key购买 nike

    我是第一次使用 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')
    );

    最佳答案

    问题在于,由于服务器已经呈现了用户列表,所以它有一个带有 liul。但是当客户端加载时没有初始数据,所以只有 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 的第二个参数,即初始状态。

    关于javascript - React/Redux 服务器端渲染中的警告 : Did not expect server HTML to contain a <li> in <ul>.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47017424/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com