简体   繁体   English

当我更新它的值时更改道具

[英]Change prop when i update its value

so I have looked around but nothing seemed to fix my problem.所以我环顾四周,但似乎没有什么能解决我的问题。 I have something with react that has likes but the child component that shows the likes and also adds the likes does only update after refreshing.我有一些带有喜欢的反应,但显示喜欢并添加喜欢的子组件只会在刷新后更新。 I tried useEffect things also but does not seem to work.我也尝试了 useEffect 东西,但似乎不起作用。

function updateLike(vacation, update) {
    if(update === 0) {
      vacation.likes--;
    } else if (update === 1) {
      vacation.likes++;
    }
  }

This is my function in App.js that gets triggered when someone clicks the like button.这是我在 App.js 中的 function,当有人点击赞按钮时会被触发。

import { faHeart, faTimes } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import React from 'react'

const vacationInfo = ({ vacation, setVacation, updateLike }) => {
    return (
        <div className="vacation">
            <div className="container">
                <div className="row">
                    <div className="vacation__banner">
                        <img src={vacation.images.image1} alt="" className="vacation__banner--img" />
                        <img src={vacation.images.image2} alt="" className="vacation__banner--img" />
                        <img src={vacation.images.image3} alt="" className="vacation__banner--img vacation__banner--last" />
                        <p onClick={() => setVacation(false)} className="close__button"><FontAwesomeIcon icon={faTimes} /></p>
                    </div>
                    <div className="vacation__pricetag">
                        {
                            vacation.price > 1000 ?
                            <div className="vacation__pricetag--expensive">
                                <p className="vacation__pricetag--para">EXPENSIVE</p>
                            </div>
                            : 
                            <div className="vacation__pricetag--cheap">
                                <p className="vacation__pricetag--para">CHEAP</p>
                            </div>
                        }
                    </div>
                    <div className="vacation__description">
                        <h2 className="vacation__description--title">{vacation.name}</h2>
                        <p className="vacation__description--capital">Capital: {vacation.capital}</p>

                        <p className="vacation__description--para">{vacation.description}</p>
                        <div className="vacation__description--likes">
                            HERE <p onClick={() => updateLike(vacation, 1)} className="likes__add"><FontAwesomeIcon icon={faHeart} />{vacation.likes}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default vacationInfo;

This is where you can see that i have ap tag that runs the function on click and that all works but it does not update the prop that is behind it.在这里你可以看到我有一个 ap 标签,它在点击时运行 function 并且一切正常,但它不会更新它背后的道具。

You forgot to update your state by using setVacation .您忘记使用 setVacation 更新您的setVacation

Also be careful with the mutation也要小心突变

So所以

function updateLike(vacation, update) {
    if(update === 0) {
      vacation.likes--;
    } else if (update === 1) {
      vacation.likes++;
    }
  }

should be应该

function updateLike(vacation, update) {
  let newVacation = JSON.parse(JSON.stringify(vacation));
  if (update === 0) {
    newVacation.likes--;
  } else if (update === 1) {
    newVacation.likes++;
  }
 
  setVacation(newVacation);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM