react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Child component is not rerendering on state change in ajax response from parent component

Open dspoonia7 opened this issue 10 years ago • 7 comments

I am using es6 syntax. The child (DataTable) component is not rerendering (updating) after state change in ajax call response in parent component.`

    class DataTable extends React.Component {
      constructor(props) {
        super(props);
        this._dataList = this.props.data;
        this._defaultSortIndexes = [];
        var size = this._dataList.length;
        for (var index = 0; index < size; index++) {
          this._defaultSortIndexes.push(index);
        }
        this.state = {
          sortedDataList: this._dataList
        };
      }
      render() {
        var {sortedDataList, colSortDirs} = this.state;
        return (
          <Table
            rowHeight={50}
            rowsCount={sortedDataList.length}
            headerHeight={50}
            width={1300}
            height={500}
            {...this.props}>
            <Column
              columnKey="created_by"
              header={
                <SortHeaderCell
                  onSortChange={this._onSortChange}
                  sortDir={colSortDirs.created_by}>
                  Created By
                </SortHeaderCell>
              }
              cell={<TextCell data={sortedDataList} />}
              width={200}
            />
          </Table>
        );
      }
    }

    class ActivityLog extends React.Component {
      constructor(props) {
        super(props);
        this.state = { 
          data: [] 
        };
      }

      getActivityLogData = () => {
        var params = {
          start: "2016-03-01",
          end: "2016-03-15"
        }
        var self = this;
        $.ajax({
          url: '/activity_log/stats',
          method: 'POST',
          data: params,
          beforeSend: (xhr) => {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
          success: (response) => {
            self.setState({data: response});
          }
        });
      }

      componentDidMount() {
        this.getActivityLogData();
      }

      render() {
        return (
          <div><DataTable data={this.state.data} />
        );
      }
    }
    module.exports = ActivityLog;

dspoonia7 avatar Aug 09 '16 20:08 dspoonia7

DataTable sets its state during the constructor, so it's right for the first render. But on subsequent renders, DataTable receives new props. constructor is not called again, so state is not updated.

I use createClass, so I would use componentWillReceiveProps, I don't know the class-based equivalent!

rmosolgo avatar Aug 09 '16 20:08 rmosolgo

Hey @rmosolgo, Actually componentWillReceiveProps is working with es6 classes also. I added this to child component and it worked fine.

componentWillReceiveProps(nextProps) {
    this.setState({sortedDataList: nextProps.data});
  }

Thanks :)

dspoonia7 avatar Aug 10 '16 06:08 dspoonia7

But now I am getting this error Uncaught Invariant Violation: Minified React error #119 Any idea?

dspoonia7 avatar Aug 10 '16 07:08 dspoonia7

image

I don't see any refs in the above code, so maybe one of the child components is creating a ref?

rmosolgo avatar Aug 10 '16 13:08 rmosolgo

Sometimes I am also getting this error

Uncaught TypeError: Cannot read property 'releaseMouseMoves' of undefined

I think this is multiple copies of react loaded issue. Is there any way to know if react is loading multiple times and if it is, then how can i resolve this?

dspoonia7 avatar Aug 14 '16 14:08 dspoonia7

any way to know if react is loading multiple times

yes, here are a couple of approaches:

  • look at your build process (or processes) are you using webpack and/or sprockets? if you're using both, make sure they're sharing a copy of React, not each one importing a separate copy
  • look at the assets you're bundling -- for example, do any of your 3rd party libraries have a copy of React inside them?

rmosolgo avatar Aug 14 '16 21:08 rmosolgo

I hope you found something that worked for you!

rmosolgo avatar Dec 09 '16 22:12 rmosolgo