Expected corresponding JSX closing tag for input Reactjs
Asked Answered
P

8

103

While creating a component in Reactjs with input fields error occurs Error: Parse Error: Line 47: Expected corresponding JSX closing tag for input at http://localhost/chat-react/src/script.js:47:20 </div>

var Main = React.createClass({
    render: function() {
        return (
            <div className="card-action">
                <i class="mdi-action-account-circle prefix"></i>
                <input id="icon_prefix" type="text" class="validate">
            </div>
        );
    }
});
Potential answered 15/6, 2015 at 18:45 Comment(1)
Sometimes the error comes from the form, which has non ending inputs.Dialectology
A
195

You need to close the input element with a /> at the end.

<input id="icon_prefix" type="text" class="validate" />
Agar answered 15/6, 2015 at 18:47 Comment(4)
Can I ask why?, (I'm new in this topic)Commission
@pedro If a tag is empty (no children elements) this is a shorthand way to close the element without having to create a matching closing element. So instead of: <div></div> . you can just do <div />. See here for more: reactjs.org/docs/…Slay
I had a <br> and this was causing the problem (it expected a <br /> instead). This is weird because HTML5 specs advises to don't use the leading slash for self closing tagsSpacesuit
@CristianTraìna It's important to remember that JSX is not HTML. JSX is it's own thing. Behind the scenes everything is converted to React.createElement and it depends on the closing of each element to know how things are intended to be nested.Agar
D
20

It happens when we do not close a html tag.

Make sure all the html tags are closed.

In my case it was the <br> tag. It should be <br />.

Try temporarily removing piece of code until you find which html tag closing is missing.

Devan answered 22/7, 2019 at 10:15 Comment(0)
C
9

This error also happens if you have got the order of your components wrong.

Example: this wrong:

 <ComponentA> 
    <ComponentB> 

    </ComponentA> 
 </ComponentB> 

correct way:

  <ComponentA> 
    <ComponentB>

    </ComponentB>  
  </ComponentA> 
Czarevitch answered 9/12, 2018 at 21:38 Comment(0)
C
4

All tags must have enclosing tags. In my case, the hr and input elements weren't closed properly.

Parent Error was: JSX element 'div' has no corresponding closing tag, due to code below:

<hr class="my-4">
<input
  type="password"
  id="inputPassword"
  class="form-control"
  placeholder="Password"
  required
 >

Fix:

<hr class="my-4"/>
<input
 type="password"
 id="inputPassword"
 class="form-control"
 placeholder="Password"
 required
/>

The parent elements will show errors due to child element errors. Therefore, start investigating from most inner elements up to the parent ones.

Clobber answered 19/10, 2019 at 8:32 Comment(0)
T
4

You need to close the input element with /> at the end. In React, we have to close every element. Your code should be:

<input id="whatever-id" type="text" class="validate" />
Transcontinental answered 29/7, 2020 at 6:40 Comment(1)
I think you meant class="validate" /> instead of class="validate/"> at the endNianiabi
H
0

You have to close all tags like , etc for this to not show.

Homologue answered 21/6, 2019 at 11:58 Comment(1)
Please provide an explanation of how and why this solves the problem would really help to improve the quality of your postPermit
B
0

All these answers are valid but I had that issue multiple times today with well closed tags. If it happens to you, just abort and run npm start again.

Bazaar answered 8/9, 2021 at 17:22 Comment(0)
A
0

Be careful with the:

<h1>aergawergerg</h1>
<h1>aergawergerg<h1/>

beginner error.

Aeroplane answered 27/3, 2023 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.