reactjs React中MUI的芯片组件在第一次单击时未呈现

ryevplcw  于 2022-11-22  发布在  React
关注(0)|答案(1)|浏览(187)

我正在使用MUI选择多选选项,并显示一些修改后的最终值作为MUI芯片。现在,当我点击添加芯片不显示在那一瞬间,但如果我改变任何值的名称和后缀的网页渲染,然后芯片显示。也许我错过了什么。任何帮助是感激。

import { useState,  } from 'react';
import styled from 'styled-components';
import {OutlinedInput, MenuItem, Chip, useTheme, Select} from '@mui/material';

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const MenuProps = {
  PaperProps: {
    style: {
      maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
      width: 250,
    },
  },
};

const EditUser =styled.div`
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-left:5px;
  margin-top: 10px;
  margin-bottom: 20px;
`
const Label = styled.label`
    width: 100px;
    margin: 10px;
`
const LabelInputWrapper = styled.div`
    margin-bottom:25px;
    flex:1 0 30%;
    display: ${(props) => props.disp};

`
const Adduserformcont = styled.div`
  display: flex;
  flex-wrap:wrap;
  margin:10px;
`
const AddChrgWrapper = styled.div`
    display: flex;
`

const names = [
  'Oliver Hansen',
  'Van Henry',
  'April Tucker',
  'Ralph Hubbard',
  'Omar Alexander',
  'Carlos Abbott',
  'Miriam Wagner',
  'Bradley Wilkerson',
  'Virginia Andrews',
  'Kelly Snyder',
];

function getStyles(name, personName, theme) {
  return {
    fontWeight:
      personName.indexOf(name) === -1
        ? theme.typography.fontWeightRegular
        : theme.typography.fontWeightMedium,
  };
}
const desigSuffix = ["I","II", "III", "IV"];

const EditUsers = () => {
  const theme = useTheme();
  const [personName, setPersonName] = useState([]);
  const [value, setValue] = useState({})
  const [finalValue, setFinalValue] =useState([])

  
  const onChangeUserForm = (event) => {
    if(event.target.name === 'selectMultiAddnCharg'){
      setPersonName(event.target.value)
    }
    let temp = {...value, [event.target.name]:event.target.value}
    setValue(temp);
    console.log(temp)
  
  }
  const addWithSuffix = () => {
    let tempArray = finalValue;
    tempArray.push(`${value.adnCharge} (${value.desgnSuffix})`)
    setFinalValue(tempArray)
    
  } 

  return (

    <>

    <form> 

        <AddChrgWrapper>
        <Label>Select name</Label>
        <Select
          name='selectMultiAddnCharg'
          multiple
          displayEmpty
          value={personName}
          onChange={onChangeUserForm}
          input={<OutlinedInput />}
          renderValue={(selected) => {
            if (selected.length === 0) {
              return <em>Select name</em>;
            }

            return selected.join(', ');
          }}
          MenuProps={MenuProps}
          inputProps={{ 'aria-label': 'Without label' }}
        >
          <MenuItem disabled value="">
            <em>Placeholder</em>
          </MenuItem>
          {names.map((name) => (
            <MenuItem
              key={name}
              value={name}
              style={getStyles(name, personName, theme)}
            >
              {name}
            </MenuItem>
          ))}
        </Select>
        </AddChrgWrapper>
      <LabelInputWrapper name="adnCharge">
                <Label>Name</Label>
                <select name="adnCharge" onChange={onChangeUserForm}>                
                    <option>Select</option>
                    {personName.map((item,index) => <option key={index}>{item}</option>)}
                </select>  
      </LabelInputWrapper>
      <LabelInputWrapper name="desgnSuffix" >
                <Label>Suffix</Label>
                <select name="desgnSuffix" onChange={onChangeUserForm}>                
                    <option>Select</option>
                    {desigSuffix.map((item,index) => <option key={index}>{item}</option>)}
                </select>  
      </LabelInputWrapper> 
      <button type="button" onClick={(e)=>addWithSuffix(e)}>Add</button>
      <div>{finalValue.map((item, index)=><Chip key={index} label={item}/>)}</div>   
      </form>
    </>
  )
}

export default EditUsers
0ve6wy6x

0ve6wy6x1#

addWithSuffix函数中,当你声明tempArray时,它不是一个新数组,但你仍然直接改变finalValue的状态,这是不允许的。
state是在应用更改时对组件状态的引用。它不应该直接变化。相反,应该通过基于state的输入构建新对象来表示更改。
可以基于旧状态声明新数组变量
let tempArray = [...finalValue]
或使用setFinalValue的第一个参数(旧状态)

const addWithSuffix = () => {
    let tempArray = [...finalValue];
    tempArray.push(`${value.adnCharge} (${value.desgnSuffix})`)
    setFinalValue(tempArray)
}

const addWithSuffix = () => {
   setFinalValue(state => state.concat(`${value.adnCharge} (${value.desgnSuffix})`))
}

示范:

第一次

相关问题