简体   繁体   English

ReactJS:组件不会在状态更改时重新呈现

[英]ReactJS: Component doesn't rerender on state change

I am trying to update state on click event using react hooks.我正在尝试使用反应钩子更新点击事件的状态。 State changes, but component doesn't rerender.状态发生变化,但组件不会重新渲染。 Here is my code snippet:这是我的代码片段:

function ThirdPage() {
const [selectedIngredients, setSelectedIngredients] = useState([])

const DeleteIngredient = (ingredient) => {
    let selectedIngredientsContainer = selectedIngredients;
    selectedIngredientsContainer.splice(selectedIngredientsContainer.indexOf(ingredient), 1);
    setSelectedIngredients(selectedIngredientsContainer);
    console.log(selectedIngredients);
}


const selectedIngredientsDiv = selectedIngredients.map(ingredient =>
    (
    <div className={styles.selectedIngredientsDiv}>{ingredient}
                                                    <div className={styles.DeleteIngredient}
                                                        onClick={() => {
                                                        DeleteIngredient(ingredient)}}>x</div></div>
    ))

return (
...

What am I doing wrong?我究竟做错了什么? Thanks in advance!提前致谢!

Issue with you splice as its not being saved to selectedIngredientsContainer.您的拼接问题,因为它没有保存到 selectedIngredientsContainer。 I would do following:我会做以下事情:

selectedIngredientsContainer = selectedIngredientsContainer.filter(value => value !== ingredient);

or或者

selectedIngredientsContainer.splice(selectedIngredientsContainer.indexOf(ingredient), 1 );
setSelectedIngredients([...selectedIngredientsContainer]);

Hope it helps.希望能帮助到你。

normally I would leave an explanation on what's going on but tldr is that you should check first to make sure that you're array isn't empty, then you you can filter out the currentIngredients.通常我会解释发生了什么,但 tldr 是你应该首先检查以确保你的数组不为空,然后你可以过滤掉 currentIngredients。 Also you don't need curly brackets to call that function in the jsx but that can be personal flavor for personal code.此外,您不需要大括号在 jsx 中调用该函数,但这可以是个人代码的个人风格。 I apologize if this doesn't help but I have to head out to work.如果这没有帮助,我很抱歉,但我必须出去工作。 Good luck!祝你好运!

function ThirdPage() {
  const [selectedIngredients, setSelectedIngredients] = useState([]);

  const DeleteIngredient = ingredient => {
    // let selectedIngredientsContainer = selectedIngredients;
    // selectedIngredientsContainer.splice(selectedIngredientsContainer.indexOf(ingredient), 1);
    // setSelectedIngredients(selectedIngredientsContainer);
    // console.log(selectedIngredients);
    if (selectedIngredients.length > 0) {
      // this assumes that there is an id property but you could compare whatever you want in the Array.filter() methods
      const filteredIngredients = setSelectedIngredients.filter(selectedIngredient => selectedIngredient.id !== ingredient.id);
      setSelectedIngredients(filteredIngredients);
    }
    // nothing in ingredients - default logic so whatever you want
    // log something for your sanity so you know the array is empty
    return;
  };

  const selectedIngredientsDiv = selectedIngredients.map(ingredient => (
    <div className={styles.selectedIngredientsDiv}>
      {ingredient}
      <div className={styles.DeleteIngredient} onClick={() => DeleteIngredient(ingredient)}>
        x
      </div>
    </div>
  ));
}

答案很简单,你的状态数组 selectedIngredients 是用一个空数组初始化的,所以当你在空数组上调用 map 时,它甚至不会运行一次,因此永远不会调用 DeleteIngredient 并且你的状态不会改变,因此不会重新 -渲染发生

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

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