0

I need to add a link and label inside an input element in reactjs component. The follwoing is what I have:

render() {
  return(
    <div className="login-panel-page">
      <input type="password" id="password"
             name="password" value="" placeholder="Password" maxLength="255" autoCapitalize="off">
        <label htmlFor="password">Password</label>
        <a>Show password</a>
      </input>
    </div>
  )
}

However here is what I get:

bundle.js:9179 Uncaught 
Error: input is a void element tag 
and must neitherhave `children` nor use `dangerouslySetInnerHTML`. 
Check the render method of Login.

Can anyone help how I can achieve that?

4
  • 1
    React is helping you avoiding writing invalid markup. Input tag can't have children. Here's a guide how to use input and label - developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… Commented Feb 26, 2018 at 22:03
  • @hamed-minaee, Have you look at the following link: stackoverflow.com/questions/22752116/react-label-element Commented Feb 26, 2018 at 22:05
  • @salman.zare Yes but in that the label will be outside of the input Commented Feb 26, 2018 at 22:06
  • @salman.zare I think the only way just through css Commented Feb 26, 2018 at 22:09

1 Answer 1

1

Input can't have children, you can do something like this

Reference : https://github.com/facebook/react/issues/6241

 render() {
    return (
    <div className="login-panel-page">
                    <input type="password" id="password" name="password" value="" placeholder="Password" maxLength="255" autoCapitalize="off"/>
                    <label htmlFor="password">Password</label>
                    <a href="#">Show password</a>

    </div>
    )
Sign up to request clarification or add additional context in comments.

Comments

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.