Error encountered: Unexpected token when defining inline style in React

When attempting to prevent scrolling on the page by using style='overflow-y: auto;':

render() {
    return (
      <div style={{overflow-y: auto}}>
        <div>{this.props.title}</div>
        <div>{this.props.children}</div>
      </div>
    );
}

An error is being thrown by Webpack:

https://i.sstatic.net/8PD8M.png

Is there an alternative approach if setting overflow-y is not possible? How can we disable page scrolling in a React application (which also uses React-Bootstrap)?

Answer №1

In order to adhere to the documentation, style attributes should be written in camel case.

Whenever we normally use a hyphen (-) to separate words in CSS, it must be changed to camel case when included within a React style object.

For instance:

overflow-y -> overflowY
overflow-x -> overflowX
background-image -> backgroundImage

The styles are added as an object.

This means that CSS properties become attributes of a JavaScript object.

The hyphen (-) is not allowed in JavaScript object attributes or variable names, so it needs to be replaced with camelCase.

Answer №2

Utilize the overflowY property based on the feedback.

Answer №3

It is not recommended to use

<div style={{overflow-y: auto}}>

The correct way is to convert it into camelCase, like

<div style={{overflowY: auto}}>

For more information, please refer to the official React documentation available at Here https://reactjs.org/docs/dom-elements.html#style

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

Trigger an action when the input text is changed, specifically when the input is populated from a different source

Is there a way to trigger an action on an input when the text is changed from another input, like a datepicker? I attempted to trigger the action when I click the date on the datepicker, but it does not seem to be working. Any suggestions? See below for my ...

What is the best way to retrieve the previous state of a file field in React?

Currently, I am working on implementing an on-change handler for an audio file in a component. The goal is to load the previous state of the file when the user changes it and records the new path. Specifically, when the user enters the component, the file ...

The thumb of the range input is misaligned with the axis markers

The handle, also known as the knob in this input[type=range] codepen, is not aligning correctly with the ticks. It appears to be off by varying amounts (either left or right) depending on the value. Move the handle to see for yourself. What CSS properties ...

Saving information to a local file using JavaScript or AngularJS

As a novice in JS development, I am eager to learn how to write data to a file stored in local storage. Within my application, I collect raw data and store it in MongoDB as key-value pairs. Simultaneously, I also wish to save this data to a file in local ...

Having trouble obtaining the ref.current.offsetWidth?

I have been working on creating a contextMenu. My goal is to retrieve the offsetWidth and offsetHeight from ref.current, but when I console.log it, it shows as undefined. const ContextMenu: React.FC<ContextMenuProps> = props => { const thisCom ...

What is causing the error message 'Unexpected use of 'location' no-restricted-globals'?

When working on my reactjs code, I encountered the following issue: const { children, location: { pathname }, } = this.props; let path = location.pathname; I am also utilizing the react router module in this component. Anyone have suggestions on how ...

Verify user access rights with Vue.js Laravel

I am currently working on a project using Laravel and Vue. In my middleware, I have the following code: public function handle($request, Closure $next) { $user = auth()->user(); if ($user->hasRole('admin')) { return ...

Leveraging the reset function with the $meteor.object

Currently, I am working on a straightforward controller utilizing the angular-meteor starter project. controller("PartyDetailsCtrl", function($scope, $stateParams, $meteor) { $scope.party = $meteor.object(Parties, $stateParams.partyId); $scope.save = ...

An unexpected error occurred during page generation. Any relevant console logs will be shown in the browser's terminal window

Encountered a RangeError: Maximum call stack size exceeded while compiling this web app in the browser. The error occurred on my header tag and sometimes on the return tag. What should I do? export default function Header() { return ( <> ...

Is it possible for me to use @import to selectively choose what I need and disregard the rest?

If I am utilizing SASS and wish to incorporate a significant portion of another existing stylesheet, whether it is in SASS or CSS format, I have the option to import that particular sheet using @import, then employ @extend to apply some of its rules. Is ...

What are the best options for implementing models and controllers in Facebook's React library using ES6?

As someone who is new to the react/es6 stack and framework, I'm transitioning from developing in Backbone/Marionette.js. I've been exploring ES6 and React more recently and, coming from a background where I'm used to Backbone for Model and C ...

Retrieving data from a remote PHP server using JavaScript

I am currently working on two separate websites. Site A is coded using only HTML and JavaScript, while site B utilizes PHP. I am trying to figure out a way to access variables from site B within site A. For example: The code for site A looks something li ...

Exploring Substrings in jQuery/JavaScript

Issue Can anyone help me with locating a specific set of words within a string (gval in this scenario) that defines the specific wordset? if (gval.indexOf("define") > -1){ console.log("Hey! gval has Define"); } var gval = input.val().trim().toLowe ...

Resetting text in an input field using CSS

Here is the fiddle link for reference: http://jsfiddle.net/a765q/1/. I noticed that when I click the reset button, the text color changes to grey. Any ideas on how to fix this? ...

Display text with ellipsis on one of the two spans within a container

I'm struggling with implementing ellipsis in my HTML code. Here's what I have: <div id="wrapper"> <span id="firstText">This text should be displayed with an ellipsis</span> <span id="lastText">this text should not ...

The CSS font-weight attribute can be set to bold or light

I've encountered a strange issue with the font-weight property when applied to a button element. When set to 400, everything functions as expected. However, attempting to set it to either 300 or 500 does not produce any noticeable changes. Interesting ...

Creating a multipart/form-data request using JavaScript/jQuery

After much searching and experimentation, I have been trying to understand the process of constructing a request to be sent using something similar to $.ajax({ contentType: "multipart/form-data", type: "POST", data: ...

ShadowBox not displaying Vimeo videos

I can't figure out why my Vimeo videos are not appearing in a Shadowbox. I have followed the steps I know to be the simplest, which involve copying the example directly from the github page and then updating the shadowbox paths to match the locations ...

Running a PHP function within a PHP environment

After the user clicks the submit button and no errors are present, a token value input is generated in the script below. The goal is to post this value inside a php page for use in a query. While the input value is successfully generated, posting it to the ...

Utilizing jQuery for dynamic horizontal positioning of background images in CSS

Is there a way to set only the horizontal background property? I've tried using background-position-x and it works in Chrome, Safari, and IE, but not in Firefox or Opera. Additionally, I would like to dynamically set the left value of the position. ...