【问题标题】:react bootstrap table not rendering data without refresh反应引导表不渲染数据而不刷新
【发布时间】:2017-11-30 05:49:48
【问题描述】:

我正在使用 react-redux 和 react bootstrap table 。我的用户界面如图所示。当我添加付款时,数据保存在数据库中。我使用 componentdidupdate 和 componentdidUpdate 来呈现道具数据。但没有任何效果。该表不会在不刷新的情况下呈现数据。请帮忙................................................ ..................................................... ....

import React from 'react';
import { connect } from 'react-redux';
// import {bindActionCreators} from 'redux'
import { Link } from 'react-router';
import Messages from '../Messages';
import classnames from 'classnames';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
import { getCrmOneCustomer, saveStatus, getCrmStatus } from '../../actions/crmAction';
//--------------------------------------------------------------------------
class CrmStatus extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: '', shop_name: '', crm_id: '', status: ''
        };

        this.props.dispatch(getCrmOneCustomer(this.props.params.id));
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }


    componentDidMount() {
         this.props.dispatch(getCrmStatus(this.props.params.id));

    }

    //--------------------------------------------------------------------------
    componentWillReceiveProps(newProps) {
        console.log('componentWillReceiveProps............ from edit');
        console.log(newProps)
        this.setState({
            name: newProps.CrmOne.name,
            shop_name: newProps.CrmOne.shop_name,
            status: newProps.CrmOne.status, 

        })
      }
    //--------------------------------------------------------------------------
    handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        });
    }
    //--------------------------------------------------------------------------
    handleSubmit(event) {
        event.preventDefault();
        var Id = this.props.params.id;
        var obj = {};
        obj["crm_id"] = Id
        obj["name"] = this.state.name
        obj["shop_name"] = this.state.shop_name
        obj["status"] = this.state.status
        console.log(obj)

        this.props.dispatch(saveStatus(obj))
    }
    //--------------------------------------------------------------------------

    componentDidUpdate(){

        this.products = this.props.CrmOneStatus
    }

    render() {


        return (
            <div className="container ca-container">

                <div className="row">
                    <div className="col-md-12">
                    <h2>CRM Status</h2>


                    </div>
                </div>
                <div className="row">
            <div className="col-md-12">

            <div className ="col-md-8">
            <BootstrapTable data={this.products} striped hover pagination={true} search>
            <TableHeaderColumn isKey dataField='name'>Name</TableHeaderColumn>
            <TableHeaderColumn dataField='status'>status</TableHeaderColumn>
            <TableHeaderColumn dataField='createdAt'>Date</TableHeaderColumn>
            </BootstrapTable>
            </div>
            <div className ="col-md-4">
            <h4> Add Payment  </h4>

            <form onSubmit={this.handleSubmit} encType="multipart/form-data">

            <div className="form-group">
                  <label>
                    Name:
               </label>
                  <input disabled type="text" className="form-control" name="name" value={this.state.name} onChange={this.handleChange} placeholder="Zahid Hasan" />
                </div>
                <div className="form-group">
                  <label>
                  shop name:
                </label>
                  <input  type="text" className="form-control" name="shop_name" value={this.state.shop_name} onChange={this.handleChange} placeholder="amount" />
                </div>

              <div className="form-group">
              <label>
              Status:
              </label>
              <textarea  rows="8" cols="50"  type="text" className="form-control" name="status" value={this.state.status} onChange={this.handleChange} placeholder="comment" />
              </div>
              <div className="btn-group" role="group">
              <button type="submit" className="btn btn-success btn-lg">Submit</button>



             </div>
             </form>


             </div>
             </div>
             </div>


            </div>
        );
    }
}
// ======================== REDUX CONNECTORS ========================
const mapStateToProps = (state) => {
    return {
        CrmOne: state.crmCustomer.CrmOne,
        CrmOneStatus: state.crmCustomer.CrmOneStatus
    };
};

export default connect(mapStateToProps)(CrmStatus);

【问题讨论】:

  • 您不需要将道具分配给类变量并将其作为数据传递,您可以轻松地做到&lt;BootstrapTable data={this.props.CrmOneStatus},当您将道具分配给类变量时,重新渲染不会'不会发生,您看不到数据
  • {this.props.CrmOneStatus} 试过了,但它不起作用
  • 不起作用是什么意思,是不是有错误
  • 没有表数据没有变化。我必须刷新页面
  • 知道了,添加答案

标签: reactjs react-bootstrap-table


【解决方案1】:

您正在请求获取componentDidMount 函数中的数据,该函数仅在组件安装时执行一次,即刷新页面时它起作用的原因,但是您应该在道具更改时获取数据,即使用componentWillReceiveProps 函数。也不需要将 props 赋值给 class 变量,直接将 props 用作数据即可。

class CrmStatus extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: '', shop_name: '', crm_id: '', status: ''
        };

        this.props.dispatch(getCrmOneCustomer(this.props.params.id));
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }


    componentDidMount() {
         this.props.dispatch(getCrmStatus(this.props.params.id));

    }

    componentWillReceiveProps(newProps) {
        console.log('componentWillReceiveProps............ from edit');
        console.log(newProps)
        if(nextProps.params.id !== this.props.params.id) {
             this.props.dispatch(getCrmStatus(nextProps.params.id));
        }
        this.setState({
            name: newProps.CrmOne.name,
            shop_name: newProps.CrmOne.shop_name,
            status: newProps.CrmOne.status, 

        })
      }
    //--------------------------------------------------------------------------
    handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        });
    }
    //--------------------------------------------------------------------------
    handleSubmit(event) {
        event.preventDefault();
        var Id = this.props.params.id;
        var obj = {};
        obj["crm_id"] = Id
        obj["name"] = this.state.name
        obj["shop_name"] = this.state.shop_name
        obj["status"] = this.state.status
        console.log(obj)

        this.props.dispatch(saveStatus(obj))
    }
    //--------------------------------------------------------------------------


    render() {


        return (
            <div className="container ca-container">

                <div className="row">
                    <div className="col-md-12">
                    <h2>CRM Status</h2>


                    </div>
                </div>
                <div className="row">
            <div className="col-md-12">

            <div className ="col-md-8">
            <BootstrapTable data={this.props.CrmOneStatus} striped hover pagination={true} search>
            <TableHeaderColumn isKey dataField='name'>Name</TableHeaderColumn>
            <TableHeaderColumn dataField='status'>status</TableHeaderColumn>
            <TableHeaderColumn dataField='createdAt'>Date</TableHeaderColumn>
            </BootstrapTable>
            </div>
            <div className ="col-md-4">
            <h4> Add Payment  </h4>

            <form onSubmit={this.handleSubmit} encType="multipart/form-data">

            <div className="form-group">
                  <label>
                    Name:
               </label>
                  <input disabled type="text" className="form-control" name="name" value={this.state.name} onChange={this.handleChange} placeholder="Zahid Hasan" />
                </div>
                <div className="form-group">
                  <label>
                  shop name:
                </label>
                  <input  type="text" className="form-control" name="shop_name" value={this.state.shop_name} onChange={this.handleChange} placeholder="amount" />
                </div>

              <div className="form-group">
              <label>
              Status:
              </label>
              <textarea  rows="8" cols="50"  type="text" className="form-control" name="status" value={this.state.status} onChange={this.handleChange} placeholder="comment" />
              </div>
              <div className="btn-group" role="group">
              <button type="submit" className="btn btn-success btn-lg">Submit</button>



             </div>
             </form>


             </div>
             </div>
             </div>


            </div>
        );
    }
}

【讨论】:

  • 抱歉还是不行。必须刷新,我已经使用了你给的代码
【解决方案2】:

在构造函数中我添加了这个

this.props.dispatch(getCrmStatus(this.props.params.id));

并处理提交

this.props.dispatch(getCrmStatus(this.props.params.id));

它就像魅力一样!!!!!!!!!!!!!!!!

【讨论】:

  • 任何解释它在内部做什么?此修复程序如何解决问题?我有一个类似的问题,我的表数据来自父道具,但显示错误“nextProps.data.slice 不是函数”
猜你喜欢
  • 2017-10-11
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多