React + Redux:组件不更新

阿赞茹

尝试使用React + Redux,并且可能做起来显然很愚蠢,因为在获取数据时,触发操作以通过网络获取数据的组件不会得到更新(重新呈现)。

这是我的代码的相关位:

顶级index.js充当应用程序的入口点:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { Router, browserHistory } from 'react-router';
import reduxPromise from 'redux-promise';
import createLogger from 'redux-logger';

const logger = createLogger();

import routes from './routes';
import reducers from './reducers';

const createStoreWithMiddleware = applyMiddleware(reduxPromise, logger)(createStore);

ReactDOM.render(
  <Provider store={createStoreWithMiddleware(reducers)}>
    <Router history={browserHistory} routes={routes} />
  </Provider>
  , document.querySelector('.container'));

顶级容器应用程序:

import React, {Component} from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as Actions from '../actions';
import Header from '../components/header';
import Showcase from '../components/showcase';

function mapStateToProps(state) {
  return {
    resources: state.resources
  }
}

function mapDispatchToProps(dispatch) {
  return {
    fetchResources: () => {
      dispatch(Actions.fetchResources());
    }
  }
}


class App extends Component {

  render() {
    console.log('props in App', this.props);
    return (
      <div>
        <Header/>
        <Showcase
          fetchResources={this.props.fetchResources}
          resources={this.props.resources}
        />
      </div>
    );
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App)

组件将在要挂载并触发其显示获取的数据时触发操作以发送数据请求:

import React, {Component} from 'react';
import {connect} from 'react-redux';

class Showcase extends Component {
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    this.props.fetchResources();
  }

  render() {
    console.log('resources', this.props);
    return (
      <div>
        This is showcase
      </div>
    );
  }
}

export default connect(state => ({resources: state.resources}))(Showcase)

动作创建者:

import * as types from '../constants/ActionTypes';
import axios from 'axios';

export function fetchResources() {
  return {
    type: types.FETCH_FIRST,
    payload: axios.get('/sampledata/1.json')
  }
}

减速器的提取操作:

import * as types from '../constants/ActionTypes';

export default function resourcesReducer (state={}, action) {
  switch (action.type) {
    case types.FETCH_FIRST:
      console.log('about to return', Object.assign (state, {resources: action.payload.data }))
      return Object.assign (state, {resources: action.payload.data });
    default:
      return state
  }
};

最后是根减速器:

import { combineReducers } from 'redux';
import navigationReducer from './navigation-reducer';
import resourcesReducer from './resources-reducer';

const rootReducer = combineReducers({
  navigationReducer,
  resourcesReducer
});

export default rootReducer;

因此,这就是我正在观察的内容。请求数据的操作已成功触发,发送了请求,resolver在promise解析后收到请求,并使用获取的数据更新状态。在这一点上,我希望顶级App组件和该Showcase组件能够检测到商店已更新并重新呈现,但是在控制台中看不到它。

另外,我redux-logger对的控制台输出感到困惑

在此处输入图片说明

Specifically, I am surprized to see that the state contains reducers from the rootReducer — I don't know if it's right (an example on Redux logger Github page shows a state without reducers). It also seems surprising that the prev state as reported by redux-logger contains the same resourcesReducer object as the next state, although intuitively I would expect prev state to be more or less empty.

Could you please point out what I am doing wrong and how to get React components respond to the state changes?

==================================================

UPDATED:

1) Changed the mapStateToProps function in the App component so that it correctly maps to reducer states:

function mapStateToProps(state) {
  return {
    resources: state.resourcesReducer
  }
}

2) Still passing the resources down to the `Showcase component:

  render() {
    console.log('props in App', this.props);
    return (
      <div>
        <Header navigateActions={this.props.navigateActions}/>
        React simple starter
        <Showcase
          fetchResources={this.props.fetchResources}
          resources={this.props.resources}
        />
      </div>
    );

3) Trying to display resources on the screen by stringifying it to see what’s actually inside this object:

  render() {
    console.log('resources', this.props);
    return (
      <div>
        This is showcase {JSON.stringify(this.props.resources)}
      </div>
    );
  }

See this on the screen: This is showcase {}. The component does not seem to re-render.

Here’s the screenshot of the console showing that App’s props have updated with the values from the next state. Still, that did not cause the component to re-render:

在此处输入图片说明

UPDATED AGAIN: And my javascript-fu was poor, too. I did not quite realize that by returning Object.assign (state, {resources: action.payload.data }); I was in fact mutating the state, and that a simple inversion of arguments would let me achieve what I intended. Thanks to this discussion on SO for enlightenment.

David L. Walsh

I am surprized to see that the state contains reducers from the rootReducer

This is how it works. Take a closer look at combineReducers().

const rootReducer = combineReducers({
  navigationReducer,
  resourcesReducer
});

认识到它不是参数列表;它是单个对象参数。也许用冗长的语法更清楚:

var rootReducer = combineReducers({
  navigationReducer: navigationReducer,
  resourcesReducer: resourcesReducer
});

resourcesReducer对国家关键点返回由resourcesReducer()函数。也就是说,内的state变量resourcesReducer()只是整个状态的一部分。

传递的函数connect()将整个状态作为参数。您的实际外观应该是这样的:

export default connect(state => ({
  resources: state.resourcesReducer.resources
}))(Showcase);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React redux 不更新组件

来自分类Dev

React Redux不更新状态

来自分类Dev

即使状态无变化但React-Redux connect()也不更新组件

来自分类Dev

即使状态无变化但React-Redux connect()也不更新组件

来自分类Dev

react-redux 更新 redux 存储但不更新组件状态

来自分类Dev

React Hook不更新条件组件

来自分类Dev

Redux不更新组件

来自分类Dev

在组件更新时在Redux中保存React组件滚动位置

来自分类Dev

更新存储后,React中的useEffect不更新组件

来自分类Dev

如何优化React + Redux中嵌套组件的prop的小更新?

来自分类Dev

在redux状态的语言环境更新后重新渲染React组件

来自分类Dev

React Redux状态更新未反映到相关组件

来自分类Dev

填充Redux存储后React组件未更新

来自分类Dev

React + Redux-状态更改后组件不会更新

来自分类Dev

在redux状态的语言环境更新后重新渲染React组件

来自分类Dev

React + Redux在组件更新后做一些事情

来自分类Dev

React Native / Redux - 只能更新已安装或正在安装的组件

来自分类Dev

Redux 商店更新后,React 组件未收到道具

来自分类Dev

更改网址时,react router dom不更新类组件

来自分类Dev

在不扩展组件的类中更新 React 中的变量

来自分类Dev

为什么购物车React-Redux不更新?

来自分类Dev

React组件不渲染

来自分类Dev

功能组件上的React-Redux + Redux-Thunk。从API提取数据后更新UI

来自分类Dev

React Redux:我的React组件没有收到更新的数组作为prop

来自分类Dev

React Native-Redux状态正在更新,但组件视图未正确更新

来自分类Dev

react-redux是否可以更新子级智能组件而无需更新父级?

来自分类Dev

反应 redux 不更新视图组件

来自分类Dev

React Redux追加组件

来自分类Dev

React Redux追加组件

Related 相关文章

  1. 1

    React redux 不更新组件

  2. 2

    React Redux不更新状态

  3. 3

    即使状态无变化但React-Redux connect()也不更新组件

  4. 4

    即使状态无变化但React-Redux connect()也不更新组件

  5. 5

    react-redux 更新 redux 存储但不更新组件状态

  6. 6

    React Hook不更新条件组件

  7. 7

    Redux不更新组件

  8. 8

    在组件更新时在Redux中保存React组件滚动位置

  9. 9

    更新存储后,React中的useEffect不更新组件

  10. 10

    如何优化React + Redux中嵌套组件的prop的小更新?

  11. 11

    在redux状态的语言环境更新后重新渲染React组件

  12. 12

    React Redux状态更新未反映到相关组件

  13. 13

    填充Redux存储后React组件未更新

  14. 14

    React + Redux-状态更改后组件不会更新

  15. 15

    在redux状态的语言环境更新后重新渲染React组件

  16. 16

    React + Redux在组件更新后做一些事情

  17. 17

    React Native / Redux - 只能更新已安装或正在安装的组件

  18. 18

    Redux 商店更新后,React 组件未收到道具

  19. 19

    更改网址时,react router dom不更新类组件

  20. 20

    在不扩展组件的类中更新 React 中的变量

  21. 21

    为什么购物车React-Redux不更新?

  22. 22

    React组件不渲染

  23. 23

    功能组件上的React-Redux + Redux-Thunk。从API提取数据后更新UI

  24. 24

    React Redux:我的React组件没有收到更新的数组作为prop

  25. 25

    React Native-Redux状态正在更新,但组件视图未正确更新

  26. 26

    react-redux是否可以更新子级智能组件而无需更新父级?

  27. 27

    反应 redux 不更新视图组件

  28. 28

    React Redux追加组件

  29. 29

    React Redux追加组件

热门标签

归档