1

I am getting the following error while adding two extra button in the list using React.js.

Error:

./src/TodoItems.js
Syntax error: Adjacent JSX elements must be wrapped in an enclosing tag (10:46)
   8 |  
   9 |   createTasks(item) {
> 10 |     return <li key={item.key}>{item.text}</li><a href="" class="button bg_green">Edit</a><a href=""class="button bg_red">Delete</a>
     |                                               ^
  11 |   }
  12 |  
  13 |   render() {

I am explaining my code below.

import React, { Component } from "react";
class TodoItems extends Component {
  constructor(props, context) {
    super(props, context);

    this.createTasks = this.createTasks.bind(this);
  }

  createTasks(item) {
    return <li key={item.key}>{item.text}</li><a href="" class="button bg_green">Edit</a><a href=""class="button bg_red">Delete</a>
  }

  render() {
    var todoEntries = this.props.entries;
    var listItems = todoEntries.map(this.createTasks);

    return (
      <ul className="theList">
          {listItems}
      </ul>
    );
  }
};

export default TodoItems;

Here I am adding two button with the list i.e-li element and getting the above error. I need to resolve those error and add two button there.

6
  • first thing first, use className instead of class in JSX, and use a () to wrap your JSX after the return Commented Feb 22, 2018 at 7:10
  • @izengod : I did like return <li key={item.key}>{item.text}</li><a href="" className="button bg_green" key={item.key}>Edit</a><a href=""className="button bg_red" key={item.key}>Delete</a> () but same error. Commented Feb 22, 2018 at 7:13
  • 1
    createTasks must return a parent element, like <li key={item.key}>{item.text} <a href="" className="button bg_green" key={item.key>Edit</a><a href=""className="button bg_red" key={item.key>Delete</a></li> or wrap it under a div ... check this below : stackblitz.com/edit/react-ypqryi Commented Feb 22, 2018 at 7:19
  • Yes you are right. Commented Feb 22, 2018 at 7:23
  • Possible duplicate React - expressions must have one parent element? Commented Feb 22, 2018 at 7:24

3 Answers 3

2
import { Fragment } from 'react'

 createTasks(item) {
return 
       <Fragment>
            <li key={item.key}>{item.text}</li>
            <a href="" class="button bg_green">Edit</a>
            <a href=""class="button bg_red">Delete</a>
       </Fragment>

}

Sign up to request clarification or add additional context in comments.

Comments

1

Since you want to create a list wiche should look in pure HTML like this:

<ul>
  <li>
      Some Text
      <a href="" class="button bg_green">Edit</a>
      <a href=""class="button bg_red">Delete</a>
  </li>
  <!-- ... -->
</ul>

When you want to return a multiple components you need to wrap them inside a parent note. In this case you can use the li tag to have the correct semantic of you html. Additionaly you need to change the key word class to className.

createTasks(item) {
    return (
        <li key={item.key}>
            {item.text}
            <a href="" className="button bg_green">Edit</a>
            <a href=""className="button bg_red">Delete</a>
        </li>
    )
}

render() {
    var todoEntries = this.props.entries;
    var listItems = todoEntries.map(this.createTasks);

    return (
        <ul className="theList">
            {listItems}
        </ul>
    );
}

Working Example

class TodoItems extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.createTasks = this.createTasks.bind(this);
  }

  createTasks(item) {
    console.log(item)
    return (
        <li key={item.key}>
            {item.text}
            <a href="" className="button bg_green">Edit</a>
            <a href=""className="button bg_red">Delete</a>
        </li>
    )
  }

  render() {
    var todoEntries = this.props.entries;
    var listItems = todoEntries.map(this.createTasks);
    return (
      <ul className="theList">
          {listItems}
      </ul>
    );
  }
}

const entries = [
   {
      key: 1,
      text: "Lern some Vanila JavaScript"
   },
   {
      key: 2,
      text: "Lern some React"
   }
]

ReactDOM.render(
  <TodoItems entries={entries}/>,
  document.querySelector('#app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app"></div>

Comments

0

Try to wrap returned value in createTasks method:

  createTasks(item) {
    return 
           <div>
                <li key={item.key}>{item.text}</li>
                <a href="" class="button bg_green">Edit</a>
                <a href=""class="button bg_red">Delete</a>
           </div>
  }

3 Comments

Error gone but after submitting the li values are not displaying with both buttons.
I would suggest using <React.Fragment> instead of <div> - OP is constructing a list (see <ul> ... <li> in the DOM), and having divs as immediate children of ul wouldn't be a valid HTML.
about to write this as comment. check this out reactjs.org/docs/fragments.html

Your Answer

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