【问题标题】:React-Redux will not render, how do I troubleshoot what is going on?React-Redux 不会渲染,我该如何解决发生的问题?
【发布时间】:2017-06-29 07:56:38
【问题描述】:

我正在使用 Redux 在 React 中开发一个应用程序,当什么都没有呈现并且没有出现错误时,这令人沮丧。任何人都可以在这种情况下提供一些故障排除实践吗?

我看到一个类似的帖子:React/Redux Component will not render

我尝试在那里实施这些建议,但它对我没有任何帮助,我只是继续从浏览器中得到一个空白的凝视。

这是我的 reducers/reducer_books.js:

// Step 1. Developing Our Reducer.
// Step 2. In index.js we will wire it in so that it will work
// this is a reducer, just a function and a return with a piece of our application state, a list of books
// to be able to make use of this reducer function anywhere else in our application, we need to export it.
// with export default any other file in our application can import this one and it will just automatically
// receive the books reducer.
export default function() {
  return [ // return array
    {title: 'Coding with Javascript for Dummies'}, // these will be a couple of different books
    {title: 'Meaningful Work'},
    {title: 'Head First Javascript'},
    {title: 'How To Do Everything With Javascript'},
  ]
}

这是我的 reducers/index.js:

// Step 2. In index.js we will wire it in so that it will work
// importing Javascript objects that contain methods we need to use
// in order to use React. They are the react library.
import {combineReducers} from 'redux';
import BooksReducer from './reducer_books';

// we need to ensure that this code is generating usable state for our
// application by creating a booklist component within react
const rootReducer = combineReducers({
    // this gives us the same mapping of state
    // key is books and value is BooksReducer
    // passing this into the function combineReducers is essentially telling
    // reduce how to create our application state
    // the single piece of state is books
    books: BooksReducer
});

export default rootReducer;

这是我在 container/book-list.js 中提升为容器的组件:

// purpose of this component is to render a list of books
// we are going to use the react-redux library by making use of one of our components
// as a container instead.
// A container is a react component that has a direct connection to the state
// managed by redux.
// React and Redux are two separate libraries, this file is the melding of both
// libraries, thereby making this component aware of the state contained in Redux.
import React, {Component} from 'react';
// pulling off a single property called connect
// this is the glue between React and Redux, only through this library do we
// merge them. React-Redux in turn makes this connect function available.
import {connect} from 'react-redux';

class BookList extends Component {
  // new function
  renderList() {
    // plugin our application state as this.props.books
    return this.props.books.map((book) => { // mapping the array
        return (
          // for each book in the array, we create a list with title
          <li key={book.title} className="list-group-item">{book.title}</li> // because it is a list we have to add a key
        );
    });
  }
  render() {
    return (
      <ul className="list-group col-sm-4">
        // when calling a separate function inside of JSX, we write curly braces
        // this calls a new function of renderList
        // helper function, which is a function that helps another function
        {this.renderList()}
      </ul>
    )
  }
}
// This function is the glue between React and Redux.
// purpose of this function is to take our application state as an argument
// our state contains the array of books and the active book
// The argument is a state that returns an object.
function mapStateToProps(state) {
  // Whatever is returned will show up as props
  // inside of BookList
  return {
    // the object returned with a key of books and a value of state.books
    // because our books reducer is returning the books property, the array of
    // objects.
    books: state.books
  };
}

// the connect function says take this component, take this map.state to props
// and returns a container. The container is aware of the state in Redux.
// In a container file we do not want to export the BookList, we want to export
// the container
export default connect(mapStateToProps)(BookList);

这是书单/index.html:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="/style/style.css">
    <link rel="stylesheet" href="https://cdn.rawgit.com/twbs/bootstrap/48938155eb24b4ccdde09426066869504c6dab3c/dist/css/bootstrap.min.css">
    <script src="https://maps.googleapis.com/maps/api/js"></script>
  </head>
  <body>
    <div class="container"></div>
  </body>
  <script src="/bundle.js"></script>
</html>

这是组件/app.js:

import React from 'react';
import {Component} from 'react';

import BookList from '../containers/book-list';

export default class App extends Component {
  render() {
    return (
      <div>
        <BookList />
      </div>
    );
  }
}

添加 webpack.config.js 文件:

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

【问题讨论】:

  • 您是否尝试过使用 Chrome 调试工具进行 React 和 Redux?你使用的是开发版的 React 吗?这会给你很好的错误信息。
  • @jmargolisvt,我已经为 Chrome 开发工具安装了 React 功能,但它通常会在将应用程序识别为 React 应用程序时弹出。在这种情况下,它似乎无法将其识别为 React 应用程序。
  • 在你添加 Redux 之前这个工作有效吗?我在这里没有看到 ReactDOM。
  • @jmargolisvt,是的,在我开始开发之前,它有一个样板“react simple starter”是它之前呈现的。

标签: javascript react-redux


【解决方案1】:

在您的代码中添加一个调试器。然后打开 Chrome 开发工具面板。导航到控制台选项卡。

调试器看起来像这样(你可以把它放在任何地方,而不是在 return 语句中):

class BookList extends Component {
  // new function
  renderList() {

    debugger

    // plugin our application state as this.props.books
    return this.props.books.map((book) => { // mapping the array
        return (
          // for each book in the array, we create a list with title
          <li key={book.title} className="list-group-item">{book.title}</li> // because it is a list we have to add a key
        );
    });
  }
  //...

如果您的代码通过调试器运行,它将在控制台中停止并允许您与当前范围进行交互。

因此,您可以在控制台中输入this.props.books 并查看它是否提供了您所期望的。

检查哪些代码正在运行也很方便。

当您访问该页面时会加载任何内容吗?当您检查 html 时,请查看您的 index.html 内容是否已加载。它在页面上是不可见的,因为它没有实际内容,但可以在 Chrome 开发工具中查看。

【讨论】:

  • 嘿实验,我将调试器放在那里并在控制台中键入 this.props.books,然后我在控制台中得到了这个:未捕获的 TypeError:无法读取 处未定义的属性 'books': 1:11。要在 Elements 中回答您的问题,样式表、javascript 和容器类正在加载。
  • 这是有价值的信息。这意味着您将调试器放在哪里 this.props = undefined。您可以通过将调试器移动到导致该函数调用的函数来跟踪它,以查看您丢失道具的位置。 (或者只是添加大量调试器并观察您的应用程序的流程。)您似乎还缺少“MapPropsToState”。这对于将信息放到正确的位置可能很重要。你是碰巧运行 webpack 吗?
  • 实验,我确实添加了mapStateToProps,我还在上面添加了我的webpack.config.js文件。
  • 对不起我的错。度过了漫长的一天。我的意思是MapDispatchToProps。我真的没有看到任何与dispatchaction 对齐的东西,所以你的代码渲染的路线对我来说有点模糊,无法看到整个画面。绝对可以使用该调试器语句来查看事情是如何流动的。另外,您是如何在您的条目文件中将内容插入页面的?
  • 嘿实验,空的 src/index.js 是问题所在。我添加了中间件常量和 ReactDOM.render 的导入,瞧!感谢您的帮助和耐心。
猜你喜欢
  • 2021-02-14
  • 2017-02-10
  • 2021-03-19
  • 2021-04-06
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-02-22
相关资源
最近更新 更多