score:294
Either change your file extensions to .jsx
as mentioned or disable the jsx-filename-extension rule. You can add the following to your config to allow .js
extensions for JSX.
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
}
score:-1
For future readers who want to write jsx in .js
files:
- Install
npm i react react-dom
even if you think you're not writing react. - Install
npm i -D @babel/preset-react
- In babel config:
plugins: ['@babel/plugin-transform-react-jsx']
- you should setup
module.rules
for/\.jsx?$/
files withbabel-loader
(so you will need to installnpm i -D babel-loader
too)
score:2
To expound on Martin's answer, it seems that it is not possible, currently, to use JSX
in React Native. A PR was created but reverted due to concerns about fragmentation and unknown consequences of having things like index.ios.jsx
. I'm not sure how AirBnb works around this or if they do, but I have used basically the same rules
block as Martin.
score:4
Following React documentation:
Each JSX element is just syntactic sugar for calling React.createElement(component, props, ...children).
Following Airbnb's style guide:
Do not use React.createElement unless you’re initializing the app from a file that is not JSX.
You could do this:
//index.js
const app = React.createElement(App);
ReactDOM.render(app, document.getElementById('root'));
score:6
If you don't want to change your file extension, you can export a function that returns jsx, and then import and call that function in your js file.
// greeter.jsx
import React from 'react';
export default name => (
<div>
{`Hello, ${name}!`}
</div>
);
and then
// index.js
import ReactDOM from 'react-dom';
import greeter from './components/greeter';
const main = document.getElementsByTagName('main')[0];
ReactDOM.render(greeter('World'), main);
score:15
Call me a dummy if it does not work for you
"rules": {
"react/jsx-filename-extension": [0],
"import/extensions": "off"
}
score:28
It's work for me. hope to help you.
disable jsx-filename-extension in
.eslintrc
:"rules": { "react/jsx-filename-extension": [0] }
in
webpack.config.js
:resolve: { extensions: ['.js', '.jsx'] },
Source: stackoverflow.com
Related Query
- JSX not allowed in files with extension ' .js' with eslint-config-airbnb
- Globbing not working with gulp eslint with jsx files
- Do I have to save react component files with a jsx extension
- Plugin/Preset files are not allowed to export objects, only functions. Removing and keeping babel installs? Selection with webpack?
- React suggestions and autocomplete does not work for files with js extension in VSCode
- Preset files are not allowed to export objects
- Service mocked with Jest causes "The module factory of jest.mock() is not allowed to reference any out-of-scope variables" error
- Error with Redux DevTools Extension using TS: "Property '__REDUX_DEVTOOLS_EXTENSION_COMPOSE__' does not exist on type 'Window'."?
- ESLint with Arbnb config and Facebook Flow together
- Jest Test Babel Error: Plugin/Preset files are not allowed to export objects
- Setting up Airbnb ESLint with React and Webpack
- Require jsx files without specifying extension
- ESLint in class with fat arrow using airbnb rules
- Eslint react/jsx-one-expression-per-line: allow variables and JSX strings on the same line, but not elements
- JSX not producing expected output with SPAN nested in A
- Configuring eslint with JSX
- react-i18next not loading json translation files in React app created with create-react-app
- create-react-app with -–template typescript not creating .tsx files
- Bug ESlint with class React, example : 'state' is not defined. (no-undef)
- Rollup + React 17 with new JSX Transform - "React is not defined"
- React createContext(null) not allowed with Typescript?
- Got error: Plugin/Preset files are not allowed to export objects, only functions
- Trouble building react-webapp with ESLint; ESLint couldn't find the config "developit" to extend from
- Loading asset with custom extension not working
- webpack not reflecting changes in js files with react and django
- can i use the tsx extension for test files if using react with typescript
- CSS and Images files not coming after transpiling package with babel in new CRA
- node express Server does not serve compressed static files with brotli and gzip compression
- Yarn 3.x PNP typescript CRA eslint not working with VSCode
- Next.js pages using Link module not loading files with CSS import
More Query from same tag
- How to detect if the form has been submitted?
- Redux is not returning data from reducer after action is called
- Select class - onChange test
- ReactJS with TypeScript not rendering to the DOM
- Is there a way on how to solve corse policy?
- onChange not working in React for input box
- How can I check multiple conditions inside of UseMemo()
- When I wrap the index.js with my context provider I just see a blank page
- "Uncaught SyntaxError: Cannot use import statement outside a module" When Importing ReactJS
- Dynamic routes fail to refresh in browser?
- Why declearing function(Element) outside render doesn't work?
- Two ternary operators spread into one inline style giving 'Failed to compile' error
- message in my react app how to solve this
- pass argument to ReactJS ES6 class
- React-Hook-Form working on PC, but on mobile inputs go blank when switching fields
- Warning: Each child in a list should have a unique "key" prop. | Gatsby JS
- Why am i getting this error in my React application?
- Why does a useState value isn't updated inside useHotkeys callback?
- Y Scale domain minimum / min NOT zero for stacked bar chart; overflowing X-Axis
- How to check the entire state of a form in react, in order to enable a disabled button submit
- Jest spyOn-function returns "'_isMockFunction' of undefined"
- Importing Library into Next.js Component Issue
- Clear redux state when page transition
- Use Ruby .erb files in React?
- Chart .js Method is not implemented
- Unhandled Rejection (FirebaseError): Function DocumentReference.update() called with invalid data. Unsupported field value: a custom object
- Redirecting in react js class based components
- How to access a variable defined in a react component from another js file?
- date-fns Format Date and Age calculation problem in React
- Break axios promise chain on catch()