javascript - React setState 更新值但未显示在 UI 中

标签 javascript reactjs jsx

我是一个初学者,想编写一个小型仓储应用程序。单击 listItem 组件中的 take 或 add 按钮应调用 addAmount 或 takeAmount 函数并更改 listItem 的金额值。 console.log 提供了正确的值更改,但 UI 中的数字没有变化。你能帮我吗?

应用程序组件:

import React, { useState } from 'react';
import logo from './logoblank.svg';
import './App.css';
import { AccessAlarm, AccountBox, LocalCafe, Print } from '@material-ui/icons';
import ListItem from './ListItem/ListItem.js';
import Location from './Location/Location.js';
import Category from './Category/Category.js';
import Search from './Search/Search.js'

const App = props => {
  const [locationState, setLocationState] = useState({
    locations: [
      { name: '', picture: '' }
    ]
  });

  let [listState, setListState] = useState({
    listItems: [
      { key: '', icon: <LocalCafe />, name: 'xxx', details: 'xxx', storage: 'xxx', amount: 3, orderAction: 'xxx' },
      { key: '', icon: <Print />, name: 'xxx', details: 'xxx', storage: 'xxx', amount: 5, orderAction: 'xxx' }
    ]
  });

  let [searchState, setSearchState] = useState({value: ''});

  console.log(locationState, listState);

  const listItems = (
    <div>
      {listState.listItems.map((listItem, index) => {
        return <ListItem
          key={index}
          icon={listItem.icon}
          name={listItem.name}
          details={listItem.details}
          storage={listItem.storage}
          amount={listItem.amount}
          click1={() => addAmount(index)}
          click2={() => takeAmount(index)}/>
      })}
    </div>
  )

  let addAmount = (index) => {
    let amount = listState.listItems[index].amount;

    amount = amount + 1;

    let listCopy = listState;

    listCopy.listItems[index].amount = amount;

    setListState(listCopy);

    console.log(listState.listItems[index].amount);

    console.log(listState);
  }

  let takeAmount = (index) => {
    let amount = listState.listItems[index].amount;

    amount = amount - 1;

    let listCopy = listState;

    listCopy.listItems[index].amount = amount;

    setListState(listCopy);

    console.log(listCopy.listItems[index].amount);
  }

  let searchChangeHandler = (event) => {
    let searchValue = searchState;
    searchValue.value = event.target.value;
    setSearchState(searchValue);

    console.log(searchState);
  }


  return (
    <div className="App">
      <header className="App-header">
        <div className="App-header-left">
          <img src={logo} className="App-logo" alt="SleevesUp!" />
          <p className="pad">Warehousing</p>
        </div>
        <div className="App-header">
          <Search
          changed={(event) => searchChangeHandler(event)} />
        </div>
        <div className="App-header-right">
          <p className="pad">Roomhero</p>
          <AccountBox />
        </div>
      </header>
      <Location
        name={locationState.locations[0].name}
        picture={locationState.locations[0].picture}
      />
      <Category />
      {listItems}
    </div>
  );
}

export default App;

这是ListItem.js的代码

import React from 'react';
import './ListItem.css'

const listItem = (props) => {
    return (
        <div className="listItem">
            <div className="listItemColumn icon">
                {props.icon}
            </div>
            <div className="listItemColumn">
                <div className="name">{props.name}</div>
                <div className="details">{props.details}</div>
            </div>
            <div className="listItemColumn">
                {props.storage}
            </div>
            <div className="listItemColumnTake">
                <button className="take" onClick={props.click2}>Take</button>{props.amount}<button className="take" onClick={props.click1}>Add</button>
            </div>
            <div className="listItemColumn">
                <button className="order">Order</button>
            </div>
        </div>
    )
};

export default listItem;

最佳答案

您正在将状态的引用复制到 listCopy,从而直接改变状态。

这不会导致重新渲染。

相反,通过扩展当前对象来创建一个新对象,它应该可以正常工作。

let listCopy = {...listState};

关于javascript - React setState 更新值但未显示在 UI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60009620/

相关文章:

css - 用于 Typescript 的 SVG-React 组件

reactjs - 在我的 React 应用程序中使用 @emotion/core 时出错

dom - 如何使用 TSX 使 StencilJS 组件在没有组件标记的情况下呈现?

javascript - className 下有两个类的元素

javascript - 使用扩展运算符时如何处理未定义的 API 响应?

javascript - 如何将jsx元素存储到javascript变量

javascript - 无法用循环正确替换 DOM 中的元素

Javascript持久化>逻辑

javascript - ESLint 禁用 localStorage 和 sessionStorage

javascript - 是否可以提供一个特定的 URL 供任何人在 Web 应用程序中为页面添加书签?