简体   繁体   English

状态更新后,React Component不会重新呈现

[英]React Component not re rendering when the state got updated

The React component is not rendering when the redux store is getting updated. 更新Redux存储时,React组件未呈现。

If I am using set timeout I can able to see the data in the console. 如果使用设置超时,则可以在控制台中查看数据。 Why the component is not rendering. 为什么组件无法渲染。

What is the issue of here why my component is not getting updated. 为什么我的组件没有更新的问题是什么? I have attached the code below. 我已经附上了下面的代码。

Component 零件

 import React, { Component } from 'react'
 import { connect } from 'react-redux'
 import { bindActionCreators } from 'redux';
 import requestAPI from '../actions/getuseraction'
 class Viewdetails extends Component {
componentDidMount() {
    this.props.onGetuserlist('all')
}
render() {
    console.log("this.props.userlist")

    setTimeout(() => {
        console.log(this.props.userlist)
         this.props.userlist.map((item,i)=>{
            console.log(i)
        })
    }, 1000);
    const data = this.props.userlist.map((item,i)=>{
        return <tr>
        <td>{item.name}</td>
        <td>{item.name}</td>
        <td>{item.name}</td>
        <td>{item.name}</td>
        <td>{item.name}</td>
        <td>{item.name}</td>
    </tr>
    })
    return (
        <div>
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Age</th>
                        <th>Joindate</th>
                        <th>Country</th>
                        <th>Gender</th>
                        <th>Skills</th>
                    </tr></thead>
                <tbody>
                    {data}
                </tbody>
            </table>
        </div>
    )
 }
}
function mapStateToProps(state) {
return {
    userlist: state.userlist,
 };
}
function mapActionstoProps(dispatch) {
return {
    onGetuserlist: bindActionCreators(requestAPI, dispatch),
};
}
export default connect(mapStateToProps, mapActionstoProps)(Viewdetails)

Actions 操作

function requestAPI(type) {
console.log("requestAPI")
switch (type) {
    case 'country':
    let countryist = [];
    getcountryist()
        .then(data => countryist.push(...data.resultSet))
        .catch(reason => console.log(reason.message))
    return {
        type: 'countrylist',
        payload: countryist
    };
    case 'all':
        let userlist = [];
        getlistofuser()
            .then(data => userlist.push(...data.resultSet))
            .catch(reason => console.log(reason.message))
        return {
            type: 'userlist',
            payload: userlist
        };

    }

 }

export default requestAPI;

 async function getlistofuser() {
   let response = await fetch('https://api.myjson.com/bins/13g2pv');
   let data = await response.json();
   return data;
 }
 async function getcountryist() {
  let response = await fetch('https://api.myjson.com/bins/1g9fqv');
  let data = await response.json();
  return data;
}

Reducer 减速器

function getalluserlist(state = [], action) {
console.log("Reducer")
console.log(action.payload)
switch (action.type) {
    case 'userlist':
        return action.payload
    default:
        return state
 }
}

 export default getalluserlist;

You need to return a new object instance within the reducer. 您需要在化简器中返回一个新的对象实例。

React requires a new reference of an object to find out what state has been changed. React需要一个对象的新引用来找出已更改的状态。

When you pass action.payload you are returning the same instance after data has been fetched. 传递action.payload时,将在获取数据后返回相同的实例。

function getalluserlist(state = [], action) {
  switch (action.type) {
    case "userlist":
      return action.payload;
    default:
      return state;
  }
}

So return a new reference (I am guessing the state is shaped as [] ). 因此,返回一个新的引用(我猜状态为[] )。

function getalluserlist(state = [], action) {
  switch (action.type) {
    case "userlist":
      return [...state, action.payload];
    default:
      return state;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM