I
I
Ismail2020-04-27 21:56:47
React
Ismail, 2020-04-27 21:56:47

Why is parent not updating in ReactJS?

Good time of the day. You need to update the Parent component.
What I do: Move a card from one column to another:

moveCard

moveCard = async (type, id) => {
    const { list_id } = this.props;

    if (type === "left") {

      const response = await moveCard("left", id, list_id); //axios patch request

      this.sub.send({
        list_id: list_id,
        card: {
          id: response.id,
          created_at: response.created_at,
          updated_at: response.updated_at,
          list_id: response.list_id,
          title: response.title,
          moved_card: 'true'
        }
      });

    } else if (type === "right") {
      const response = await moveCard("right", id, list_id);

      this.sub.send({
        list_id: list_id,
        card: {
          id: response.id,
          created_at: response.created_at,
          updated_at: response.updated_at,
          list_id: response.list_id,
          title: response.title,
          moved_card: 'true'
        }
      });
    }
  }



Ie, for example, I have 2 Column components - Column 1 and Column 2. Each of them has a Card component. I am moving the card from the Column 2 component to Column 1. A request is sent to the server via the actioncable and, in theory, the Column 2 and Column 1 components should be updated. But only Column 2 is updated, i.e. the Card component is removed from it. Very confusing, but here's what should happen.

There is also a change handler for the Card component:

moveCardHandler

let ids = [];

    this.state.cards.map(card => {
      ids.push(card.id);
      return null;
    });

    const cardIndex = ids.indexOf(data.card.id);
    const cards = update(this.state.cards, { $splice:  });
    cards.shift();
    this.setState({ cards: cards });



Rails is used as a backend.
How can the Parent component be updated?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question