简体   繁体   中英

Strange Behavior with React State and MUI TextFields

I have a basic set of code.. That I am trying to toggle:

{(isLinkPhone) ?
    <Box>
        <TextField
            value={phone}
            label="Phone Number"
            disabled
        />
    </Box>
    :
    <Box>
        <TextField
            defaultValue=''
            label="Link for your button *"
        />
    </Box>
}

Basically what I am trying to do is during a toggle of:

    const [isLinkPhone, setLinkisPhone] = React.useState(false);

I want to display one or the other.. The issue is that I can't set BOTH value AND defaultValue -- I am having a hard time understanding why React is treating this as the same input that cannot have a controlled and uncontrolled value. What I am trying to do is set the field to "nothing -> enabled/editable" if !isLinkPhone -- Else set it to " {phone} -> disabled" -- The issue is that the values "translate" between toggles.

在此处输入图像描述

Is there a better way to go about this? I am hesitant to create individual components for fear I will spend the time and get the same result. Can anyone explain why this is happening?

Try this

  <Box>
    {isLinkPhone && (
      <TextField value={phone} label="Phone Number" disabled />
    )}

    {!isLinkPhone && (
      <TextField defaultValue="" label="Link for your button *" />
    )}
  </Box>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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