【问题标题】:React Modal selected data dissapears after it closesReact Modal选择的数据在关闭后消失
【发布时间】:2020-02-10 08:46:04
【问题描述】:

我有以下问题在我关闭并重新打开模式后我选择的选项消失了,有人告诉我这是因为当模式卸载时所有数据都丢失了?那么我如何使选定的数据仍然显示。

我能够检索选定的数据,但我想要的是一旦模式关闭并重新打开,选定的选项仍然存在

此外,我的选择组件不可重用任何线索我如何优化那段代码

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Modal from '@material-ui/core/Modal';
import Backdrop from '@material-ui/core/Backdrop';
import Fade from '@material-ui/core/Fade';
import Button from '../Button/Button'

const useStyles = makeStyles(theme => ({
  modal: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
  paper: {
    backgroundColor: theme.palette.background.paper,
    border: '2px solid #000',
    boxShadow: theme.shadows[5],
    padding: theme.spacing(2, 4, 3),
  },
}));

 const TransitionsModal =(props) => {
  const classes = useStyles();
  const [open, setOpen] = React.useState(false);

  const handleOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Button clicked={handleOpen}></Button>

      <Modal
        aria-labelledby="transition-modal-title"
        aria-describedby="transition-modal-description"
        className={classes.modal}
        open={open}
        onClose={handleClose}
        closeAfterTransition
        BackdropComponent={Backdrop}
        BackdropProps={{
          timeout: 500,
        }}
      >
        <Fade in={open}>
{props.children}
        </Fade>
      </Modal>
    </div>
  );
}
export default TransitionsModal

这是我的选择组件

import React from 'react';
import InputLabel from '@material-ui/core/InputLabel';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';

export default function NativeSelects(props) {

  const [value, setValue] = React.useState('');

  const handleChange = event => {

    setValue(event.target.value);
    console.log(event.target.value)
  };


  const data = props.list.map((item, id) => {
    if (item.reciter_name_eng) {

      return <option key={item.reciter_name_eng + id} value={item.id}>{item.reciter_name_eng} {item.style}</option>
    }
    //if its not null 
    if (item.name_simple) {
      return <option key={item.name_simple + id} value={item.chapter_number}>{item.chapter_number}. {item.name_simple}</option>
    }
    if (item.language_name) {
      return <option key={item.language_name + id} value={item.id}>{item.language_name} by {item.author_name}</option>
    }
    return null

  })

  return (

    <div>
      <FormControl variant="filled">
        <InputLabel htmlFor="filled-age-native-simple">{props.type}</InputLabel>
        <Select
          native
          value={value}
          onChange={(event) => { props.changed(event.target.value, props.type); handleChange(event) }}>
          <option value={null}> </option>
          }
   {data}

        </Select>
      </FormControl>

    </div>
  );
}

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

问题是你的函数组件的默认状态总是一个空字符串,即

const [value, setValue] = useState('');

当模态框关闭时,所有组件都被卸载,这意味着当模态框重新显示时,这些组件被重新安装,结果值将返回为空字符串。

您需要跟踪所选值 TransitionModel 组件,然后将其作为道具传递回 NativeSelects 以设置默认值,即

<NativeSelect defaultValue={...} />

然后在你的组件中

function NativeSelect(props) {
  const [value, setValue] = useState(props.defaultValue);
  ...
}

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 2013-01-20
    • 2019-06-26
    • 2017-12-04
    • 1970-01-01
    • 2014-02-09
    • 2017-03-04
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多