Tips for fixing Unexpected Token Error in a nextJS project

I recently started working on a brand new "next.js" project and was eager to dive into coding. However, I found myself stuck for hours trying to resolve the following error message.

To kick things off, I decided to use a reference code snippet which looked like this -

import styled from "styled-components";

function Sidebar() {
    return {

        <Container> 
            <h1> Sidebar </h1>
        </Container>;

    }
}

export default sidebar;

const Container = styled.div;

The specific error I encountered is as follows:

      x Unexpected token `<` (jsx tag start). Expected identifier, string literal, numeric literal or [ for the computed key   
   ,----
 7 | <Container>
   : ^
   `----

Naturally, I turned to Google for answers and tried tweaking various configuration files. While there were several suggested solutions, none seemed to work out for me thus far. My suspicion is that the issue lies within the babel setup, but at the moment, I'm essentially navigating in the dark.

This is how my .babelrc file looks like:

{
  "presets": ["env"],
  "plugins": [
    ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }],
    "transform-object-rest-spread",
    // "@babel/plugin-proposal-nullish-coalescing-operator", // TODO: update to babel 7
  ],
}

If anyone has any insights or guidance on tackling this problem, please share your thoughts.

Answer №1

It is important to remember that when returning a JSX expression, you must enclose it in parentheses, rather than curly braces.

function Sidebar() {
  return (
    <Container> 
      <h1> Sidebar </h1>
    </Container>
  );
}

To learn more about the significance of using parentheses in the return statement, check out this helpful resource:

Answer №2

It is essential to enclose your returned content within parentheses when using the return statement.

return (
  <Container>
    <h1> Sidebar Content </h1>
  </Container>
);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery UI to create a bouncing effect on a CSS sprite image

Want to give your social icons a bounce effect using jQuery UI Bounce? I'm following a template and some jQuery documentation, but having trouble getting it right. It seems like the sprite image for the icons might be causing the issue. Can someone h ...

Dealing with callback errors: error handling is anticipated

In my Vue web application, I have enabled ESLint and encountered an issue with the following code: myApi.get('products/12').then((prodResponse) => { state.commit('ADD_PRODUCT', {product: prodResponse.data}) }, error => { cons ...

I'm wondering why my socket.io emit isn't triggering a content update

While working on adapting the IBM angularjs tutorial here into a Yeoman angular-fullstack tutorial, I encountered a slight issue. In my version, when I vote on a Poll, the data does not refresh to display the results. I have tried extensively debugging it ...

Using PHP, JavaScript, and Bootstrap to display success or error messages for form fields through AJAX

I am working on a form where users input an 'authorisation code' that is then checked against a database using AJAX and PHP. Currently, the form displays a tick if the code is valid and a cross if it is incorrect. I would like to utilize Bootstra ...

What information is transferred when the submit button on a form is clicked?

Currently, I am utilizing node.js along with the jade templating engine. Within my jade file, I have a form structured like this: form.form-signin(style="padding-left:10px", action='/update', method='post') table.table.table-hove ...

Difficulty occurred when trying to import Polymer components

Currently, I am in the process of learning how to utilize Polymer. I meticulously followed the instructions provided exactly as specified here. However, upon reaching step 3 and adding the import for paper-checkbox, an error presented itself: Error: A c ...

AngularJS does not update values properly if there is a style applied to the parent container

I'm struggling to find the best approach to handle this situation. This is how my AngularJS code looks: <span class="something" ng-hide="onsomecondition"> {{value}} </span> .something{ text-align:left; padding:10px;} Issue: The value ...

When accessing the defaultValue property of a select element, it will result in

Here is a typical HTML dropdown menu: <select name="email" id="email"> <option value="2" selected="selected">Before redirecting to PayPal</option> <option value="1">After payment is successful</option> <opti ...

What is the best way to send multiple values along with an image when uploading to a server using AngularJS and NodeJS?

Client side: Having successfully implemented file upload functionality using AngularJS and NodeJS, I am facing an issue where I need to pass 'name' and 'email' parameters to the server along with the uploaded file. Server side: Once ...

Animating pseudo-elements using Jquery

I'm having trouble animating the :after pseudo-element of my ul. Below is the CSS code I have: #slider .mypagination ul::after { content: ""; width:25%; bottom: 0; left:0; position: absolute; border-top:1px solid #7accc8; ...

Unable to access a function from a different controller within an AngularJS Ionic framework application

How can I trigger or call the callkhs function in the LihatKhsController from the KhsController? function KhsController($scope, $rootScope){ $scope.$emit('callkhs', {param:'test'}); } I am attempting to retrieve parameter values ...

Angular 2 Element Selection: Dealing with Unrendered Elements

This form consists of three input fields: <div> <input *ngIf = "showInputs" #input1 (change)="onTfChnage(input2)" type="text"/> <input *ngIf = "showInputs" #input2 (change)="onTfChnage(input3)" type="text"/> <input *ngIf = "showInp ...

The Kenburn zoom image effect fails to function

I want to implement a zoom effect on an image using the Ken Burns effect when it is clicked. However, the code I have written does not seem to be working as expected. Here is the code snippet: $(document).ready(function () { $('.kenburns').on( ...

Refreshing a <div> element in Django, yet there is no visible update

As I utilize a barcode scanner to add objects to my array list, the data is populated after each scan depending on the scanning speed of the user. To exhibit this data, I have designed a dedicated page. My intention is to avoid refreshing the entire page b ...

Vue project encountering issue with displayed image being bound

I am facing an issue with a component that is supposed to display an image: <template> <div> <img :src="image" /> </div> </template> <script> export default { name: 'MyComponent', ...

Using Typescript with Angular 2 to Implement Google Sign-In on Websites

Currently, I am in the process of creating a website that utilizes a typical RESTful web service to manage persistence and intricate business logic. To consume this service, I am using Angular 2 with components coded in TypeScript. Instead of developing m ...

AngularJS encounters a lack of 'Access-Control-Allow-Origin' header

I have encountered an issue with my AngularJS application. I am attempting to send data to a third-party URL for storage on their server. However, when I execute the code below, I receive the following error message: XMLHttpRequest cannot load . Response t ...

Can a PHP script be executed through an Ajax event?

Is it feasible to execute the script of a PHP file when an AJAX event is triggered? Consider this scenario: on the AJAX error, could we send the data to the error.php file, record the error, notify the admin via email, and perform any other desired action ...

Attempting to extract a text string from a chunk of HTML code

My goal is to extract a text string (specifically, an article title) from a snippet of HTML code. The title in question reads "Journalist Allegedly Spied on Zoom Meetings of Rivals in Hilariously Dumb Ways." The challenge lies in the fact that the title d ...

The useForm function from react-hook-form is triggered each time a page is routed in Nextjs

Hey there, I'm just starting out with Next.js (v14) and I'm trying to create a multi-page form using react-hook-form. But I'm encountering an issue where the useForm function is being executed every time, and the defaultValues are being set ...