Git Product home page Git Product logo

Comments (8)

ollija avatar ollija commented on July 1, 2024 1

When you delete items by removing then from SortableGrid's children, make sure you're not assigning them their key-value from map-function index, because it causes the last item to disappear instead of the intended one.
This happens because SortableGrid keeps copies of the children, and removes them based on missing keys when props update. If you assign the index of a map-function as a key for a child, SortableGrid only sees that the last key (last index) is missing and removes that.

from react-native-sortable-grid.

indivisable avatar indivisable commented on July 1, 2024

First we setup our state data:

constructor() {
    super()
    this.state = {
      favorites_data:
      [{... Your JSON ...}]
     };
...

Then added a function to add children:

addChildItem(itemData){
  this.setState({
      favorites_data: this.state.favorites_data.concat(itemData)
  });
}

And in our render function we loop through the state:

renderChildren(){
  return this.state.favorites_data.map( (item, index) =>
    <View
...

Also if you are aiming for performance it is good to restrict the updates:

  shouldComponentUpdate(nextProps, nextState){
    if (this.state.favorites_data !== nextState.favorites_data) {
      return true;
    }
    return false;
  }

For removing (seems to only remove last item... looking into this)

removeChildItemAt(index){
  var tmpArray = this.state.favorites_data.slice()
  tmpArray.splice(index, 1);
  this.setState({ favorites_data: tmpArray })
}

from react-native-sortable-grid.

indivisable avatar indivisable commented on July 1, 2024

@ollija Thank you, that is issue I was experiencing (removing only last item), however when I took off the key value when I map, only one single icon renders, instead of the 10 that should render.

from react-native-sortable-grid.

ollija avatar ollija commented on July 1, 2024

Well, you need to assign them some key. Maybe increment a number each for each item and use that as a key?

from react-native-sortable-grid.

indivisable avatar indivisable commented on July 1, 2024

Hrmm not sure I fully understand, Do you mean that the internal index and external should have different IDs? And thus you target this ID instead of the internal index?

I am still trying to figure out best way to implement the removal and insertion (Adding to end works fine) of items.

from react-native-sortable-grid.

MoeMamdouh avatar MoeMamdouh commented on July 1, 2024

hello guys, i fixed it by generating random keys numbers,
key={Math.random() * 1000}
this delete the particular item not the last one.

from react-native-sortable-grid.

shopshow avatar shopshow commented on July 1, 2024

@indivisable have you solved this problem? I'm facing the same issue...

from react-native-sortable-grid.

electricfeel1979 avatar electricfeel1979 commented on July 1, 2024

so finally I got it working as well in updating adding children, key={Math.random() * index}. In my opinion I guess they need to add this on the documentation.

from react-native-sortable-grid.

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.