Giter VIP home page Giter VIP logo

Comments (7)

adammockor avatar adammockor commented on August 28, 2024 1

I was mutating data like this:

setRowChecked(value, id) {
    const data = this.state.data.map((item) => {
      if (item.id === id) {
        item.checkbox = value;
      }
      return item;
    });

    this.setState({
      data,
    });
  }

And I forgot about that item inside map function has still same reference, so map function creates new array but actual objects inside are not new. return { ...item } fixes my problem.

Thank you for your help. Nice lib by the way!

from table.

benjycui avatar benjycui commented on August 28, 2024

Just create a shallow copy and pass it to Table after you modify dataSource

from table.

adammockor avatar adammockor commented on August 28, 2024

I am doing that like: (simplified use case)

import React from 'react';
import RcTable from 'rc-table';

class Table extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      data: props.data,
      columns: props.columns,
    };
  }

 render() {
    const columns = [...this.state.columns];
    const data = [...this.state.data];

    return (<RcTable
      columns={columns}
      data={data}
    />);
 }
}

from table.

benjycui avatar benjycui commented on August 28, 2024

So, this may work.

from table.

adammockor avatar adammockor commented on August 28, 2024

Yeah, but sadly don't. I was suspicious, that data state is mutated somewhere inside Table.jsx, but I can't find what is wrong.

from table.

afc163 avatar afc163 commented on August 28, 2024

There is a tricky problem for array immutable data:

const a = { key: 'value' };
const data = [a];

const newData = [...data];
newData[0].key = 'new value';

console.log(data[0].value); // new value

I made same mistake here: ant-design/ant-design@abecc44

from table.

afc163 avatar afc163 commented on August 28, 2024
const a = { key: 'value' };
const data = [a];

const newData = [...data];
newData[0] = { ...data[0] };

console.log(data[0].value); // value

from table.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.