react-native-sortable-grid icon indicating copy to clipboard operation
react-native-sortable-grid copied to clipboard

Update child component when state changes

Open brianlaw033 opened this issue 7 years ago • 8 comments

2017-12-04 18 04 36 Any idea how can I update the child component when state changes? the numbers arent changing when the state changes

brianlaw033 avatar Dec 04 '17 10:12 brianlaw033

I am currently facing the same problem. I have this component:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import SortableGrid from 'react-native-sortable-grid'

import AddPhoto from '../components/AddPhoto'

class ImageGrid extends Component {
  static propTypes = {
    pressAddPhoto: PropTypes.func.isRequired,
    deleteImage: PropTypes.func.isRequired,
    orderImages: PropTypes.func.isRequired,
    imageFields: PropTypes.array.isRequired,
    images: PropTypes.object.isRequired,
  }
  render () {
    return (
      <SortableGrid
        itemsPerRow={4}
        onDragRelease={itemOrder => this.props.orderImages(itemOrder.itemOrder)}
        doubleTapTreshold={300}
      >
        {this.props.imageFields.map((fieldname, index) =>
          <AddPhoto
            key={index}
            onTap={() => this.props.pressAddPhoto()}
            onDoubleTap={() => this.props.deleteImage(index)}
            source={this.props.images[fieldname]}
          />,
        )}
      </SortableGrid>
    )
  }
}

export default ImageGrid

What should happen

When tapping an item in the grid once, it opens the camera roll. I select an image and the image is saved. Through saving the new image in the parent component, the "images" prop updates. The image should be visible in the grid.

What actually happens

The state change is successful, the prop is updated, but the image is not visible.

What I've tried

I replaced the SortableGrid component with a View and when the app hot reloads, it instantly shows the images as it's supposed to. When I put SortableGrid back (and it re-renders again), it does show the right images still. I tried to call forceUpdate() on the ImagedGrid component, on the SortableGrid and on the AddPhoto components, but it didn't help.

dkaufhold avatar Dec 05 '17 13:12 dkaufhold

Never mind. It works when installing the latest commit! 👍

FYI: @brianlaw033

dkaufhold avatar Dec 05 '17 14:12 dkaufhold

I have tried, but it didn't work.Could you tell me how you did it ?@dkaufhold

Wowoy avatar Aug 08 '19 10:08 Wowoy

So have you solved this problem?@brianlaw033

Wowoy avatar Aug 08 '19 10:08 Wowoy

@Wowoy npm install --save git://github.com/ollija/react-native-sortable-grid.git will install the latest commit on the default branch.

dkaufhold avatar Aug 08 '19 10:08 dkaufhold

Ok,it works.Thank you very much!@dkaufhold

Wowoy avatar Aug 09 '19 10:08 Wowoy

has the latest commit been merged into master?

Yandamuri avatar May 15 '20 05:05 Yandamuri

不要用index 做key,就解决,但是没有删除动画

fairySusan avatar Jun 06 '20 02:06 fairySusan