0

So I've tried the following, I can't seem to change the type of an input. So the same input can either be of type text or password but I can't switch between both. This is a Semantic UI Input. (from semantic-ui-react)

   const [inputType, setInputType] = useState('password'||'text')

in my JSX:

   <Input type={inputType} value={inputValue} onChange={handleInput} className="landingInput" placeholder={inputPlaceholder} />

at initialization:

  setInputType('text'); 

after an event:

  setInputType('password'); // should update html template with type="password" but it doesn't

useEffect is used to make sure state is updated, every other hook works as expected. Could this be a security precaution? Can you think of a way to avoid creating a new input?

Thank you

2
  • should update html template with type="password" ... Which template? It seems like much relevant code is missing from this question. Commented Feb 25, 2020 at 11:13
  • 1
    I mean the generated html template, I've updated the question with the relevant JSX Commented Feb 25, 2020 at 11:14

1 Answer 1

2

Toggling input type is easy, Here I used a button to toggle the input type to text and password. check the working demo Here.

Check the updated code block

const App=()=>{
  const [inputType, setInputType] = useState('text');
  const inputPlaceholder = 'Add Here';
  const handleInput =()=>{}

  const toggleInput = ()=>{
setInputType(inputType === 'password' ? 'text': 'password')
  }
    return (
      <div>
        <input type={inputType} value="password" onChange={handleInput} className="landingInput" placeholder={inputPlaceholder} />
        <button onClick={toggleInput} >Toggle type</button>
      </div>
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

setInputType(inputType === 'password' ? 'text': 'password') did the trick. thank you very much
@van do you need the semantic UI fiddle ??. I can do that also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.