道具只在我刷新页面

时间:2018-04-17 03:11:30

标签: javascript reactjs redux react-thunk

我有100多个<td>来渲染。我使用NodeJS后端获取它,将数据传递给前端,并使用Redux来反应组件。 我有很多组件可以渲染更少的数据,而且一切都很好,但是当涉及到这个组件时,在刷新页面之前没有任何反应。

我尝试使用不同的生命周期(cpmponentWillMount,willRecieveProps等),但似乎没有任何效果。

我也使用React-thunk和react-router

第一次刷新后,不再需要了。所以基本上它只发生在我第一次启动开发服务器时。

我坚信我必须使用其中一个组件生命周期函数,并且我尝试了错误的或错误的顺序?

ReportsData ==&gt;渲染发生的地方

class ReportsData extends Component {
constructor(props) {
    super(props);
    this.state = {
        allReports: []
}}

getReportsData = () => {
    return reportsApi.getReports().then(report => {
        this.setState(() => ({
            allReports: Object.assign([], report.data)
        })
    )});
}

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

render() {

// const { reports } = this.props;

const { allReports } = this.state; 

let sortedReports = _.sortBy(reports, function(o) { return new moment(o.date) }).reverse();

const listReports = sortedReports.map(item => {
    return (

    <tr key={item.rowNumber}>
        <td> {item.year}</td>
        <td> {item.month}</td>
        <td> {item.bruto_ukupno}</td>
        <td> {item.neto_plata}</td>
        <td> {item.topli_obrok}</td>
        <td> {item.doprinosi}</td>
        <td> {parseInt(item.ukupno_plata, 10)}</td>

        <td className="table-actions">
            <Link to={{ pathname: '/reports/details', state: { item } }}>
                <PieChart size="21"/>
            </Link>
        </td>
    </tr>
)});

    return (

    <div className="portlet-body">
        <table className="table table-striped">
            <thead>
            <tr>
                <th>Year</th>
                <th>Month</th>
                <th>Gross</th>
                <th>Net</th>
                <th>Meals</th>
                <th>Taxes</th>
                <th>Salarys</th>
                <th>Details</th>
            </tr>
            </thead>
            <tbody>
                {listReports} 
            </tbody>
        </table>
    </div>
    );
}

}

ReportsAPI

    import axios from 'axios';
    import {baseApiUrl} from '../config/config';

    const reportsApiUrl = baseApiUrl + 'reports/'

    class ReportsApi {

        static getReports() {
            return axios({
                method: 'get',
                url: reportsApiUrl + 'getReports'
            })
        }
    }

export default ReportsApi;

reportsActions

    import * as actionTypes from '../actionTypes/actionTypes';
    import ReportsApi from "../api/reportsApi";

    export const getReports = (data) => {
        return {
            type: actionTypes.GET_REPORTS,
            data
        }
    }

export const getReportsAsync = () => {
    return dispatch => {
        return ReportsApi.getReports()
        .then(reports => {
            dispatch(getReports(reports.data));
        }).catch(error => {
            console.log('error while loading reports', error);
            throw(error);
        });
    };
}

仅在我第一次启动DEV服务器时才会发生这种情况 因此,当我提升应用程序时,我的应用程序每个其他组件都会收到它的道具,除了这个,我在所有地方使用相同的逻辑。唯一的区别在于渲染的数据量。如果我退出并重新登录,一切都很好。如果我刷新,继续前进,它一切都很好,但是一旦我停止服务器,再次启动它,只有这个组件有这个问题。

3 个答案:

答案 0 :(得分:1)

您在哪里导入和使用此组件。如果您首次渲染此组件时可能不存在传递ReportData的数据。在componentDidMount()生命周期方法中抛出console.log(this.props),以确定在第一次呈现组件时究竟是什么this.props

答案 1 :(得分:0)

尝试didMount和willRecieveProps?

答案 2 :(得分:0)

如果来自API的数据未定义且Redux必须等待所需数据,则会进行不必要的检查。在检出时,组件已经呈现而没有任何数据。我稍微重新安排了代码,现在它正在工作。谢谢你指点我正确的方向。

因此,问题出现在reportsService模块的后端。

以下是将来参考的修复方法:

    const _ = require('lodash');

    module.exports = {
        mapReportsSheetToJson(data) {
          let result = [];
          for (let i = 2; i < data.length; i++) {
            let elem = {};
            data[1].map((item, index) => {
              // if (typeof data[i][index] !== 'undefined') {
                elem[item.toLocaleLowerCase()] = data[i][index];
              // }
            })
            elem['rowNumber'] = i + 1;
            result.push(elem);
          }
          return result;
        }
      }