How to pass state in browserHistory.push(path, [state]) - reactjs

I'm working on a project that uses react-router 3.0.5 and I'm attempting to pass state through browserHistory.push and the route changes but the state doesn't get passed through to props in any location.
I've read the documentation but I could be missing something simple. To my knowledge, this should be sending state without an issue.
async handleSubmit(e) {
e.preventDefault();
const email = this.email.value;
const password = this.password.value;
const passwordconfirm = this.passwordconfirm.value;
const profile = {
firstName: this.firstName.value,
lastName: this.lastName.value,
firstTime: !!this.firstTime.value,
dueDate: date,
couponCode,
membership: { level: 'gold' },
};
Accounts.createUser({ email, password, profile }, (err) => {
if (err) {
return self.setError(err.message);
}
if (isGift) {
claimGift.call({ code: couponCode }, () => {
swal({
title: 'Gift Claimed!',
icon: 'success',
button: 'Continue',
}).then((confirm) => {
if (confirm) return browserHistory.push('/payment', { level: 'gold' });
});
});
} else return browserHistory.push('/payment', { level: 'gold' });
});
}
}
package.json
{
"name": "fakename",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "meteor --settings dev-settings.json"
},
"dependencies": {
"autoprefixer": "^7.1.1",
"babel-runtime": "^6.26.0",
"bcrypt": "^1.0.3",
"bluebird": "^3.5.1",
"contentful": "^5.1.3",
"marked": "^0.3.6",
"meteor-node-stubs": "~0.2.4",
"moment": "^2.21.0",
"prop-types": "^15.6.0",
"radium": "^0.19.6",
"react": "^16.1.1",
"react-async-script-loader": "^0.3.0",
"react-burger-menu": "^2.1.11",
"react-document-title": "^2.0.3",
"react-dom": "^16.1.1",
"react-feather": "^1.0.7",
"react-flatpickr": "^3.6.3",
"react-router": "3.0.5",
"react-stripe-elements": "^1.2.1",
"react-tooltip": "^3.4.0",
"reactable": "github:vladnicula/reactable",
"sib-api-v3-sdk": "^3.1.6",
"simpl-schema": "^0.3.2",
"stripe": "^5.5.0",
"sweetalert": "^2.0.8",
"sweetalert2": "^7.1.2",
"underscore": "^1.8.3"
},
"devDependencies": {
"#meteorjs/eslint-config-meteor": "^1.0.5",
"babel-eslint": "^7.2.3",
"eslint": "^4.4.1",
"eslint-config-airbnb": "^15.1.0",
"eslint-import-resolver-meteor": "^0.4.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-meteor": "^4.0.1",
"eslint-plugin-react": "^7.2.1"
},
"postcss": {
"plugins": {
"postcss-easy-import": {
"extensions": [
".css",
".scss",
".import.css"
]
},
"autoprefixer": {
"browsers": [
"last 2 versions"
]
}
}
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"allowImportExportEverywhere": true,
"allowAfterThis": true
},
"plugins": [
"meteor"
],
"extends": [
"airbnb",
"plugin:meteor/recommended"
],
"settings": {
"import/resolver": "meteor"
},
"rules": {
"import/extensions": [
"off",
"never"
],
"import/no-extraneous-dependencies": "off",
"import/prefer-default-export": "off",
"import/no-absolute-path": "off",
"react/prefer-stateless-function": "off",
"react/jsx-filename-extension": "off",
"react/forbid-prop-types": "off",
"react/require-default-props": "off",
"no-underscore-dangle": "off",
"jsx-a11y/no-static-element-interactions": "off",
"class-methods-use-this": "off"
}
}
}

React Router allows us to pass state via browserHistory and Link. Example code snippet is given below.
browserHistory.push({pathname: '/navigationPath', state: {message: 'navigated'}})
In your case, you have not added pathname and state in browserHistoy. Your code should look like this
browserHistory.push({pathname: '/payment', state: {level: 'gold'}})
Hope this solution helps you.

Related

tailwind 3.0.23 classes not being applied in real time in storybook

not able to apply tailwind classes via storybook UI in real time, for example, changing the color of a button through the props classes here from bg-red-600 to bg-red-100 doesn't change the color of the button in the storybook ui, is it possible to change the color of the button from the UI to see what it would look like? (component works as expected in app, just not in the storybook)
not sure if this is related to purging or JIT compilation https://github.com/tailwindlabs/tailwindcss/discussions/6347
storybook ui
button component
<button
type="button"
className={clsx(
"rounded-full py-2 px-3",
classes,
disabled ? "disabled:opacity-25" : ""
)}
onClick={onClick}
>
tailwind.config.js
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
};
button.stories.tsx
ButtonComponent.args = {
label: "Button",
classes: "bg-red-600",
disabled: false,
};
preview.js
import "!style-loader!css-loader!postcss-loader!tailwindcss/tailwind.css";
import "../styles/globals.css";
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
main.js
module.exports = {
stories: [
"../components/**/*.stories.mdx",
"../components/**/*.stories.#(js|jsx|ts|tsx)",
],
addons: [
"#storybook/addon-links",
"#storybook/addon-essentials",
{
name: "#storybook/addon-postcss",
options: {
cssLoaderOptions: {
// When you have splitted your css over multiple files
// and use #import('./other-styles.css')
importLoaders: 1,
},
postcssLoaderOptions: {
// When using postCSS 8
implementation: require("postcss"),
},
},
},
],
framework: "#storybook/react",
staticDirs: ["../public"],
};
package.json dev dependencies
"devDependencies": {
"#babel/core": "^7.17.7",
"#babel/eslint-parser": "^7.17.0",
"#fullhuman/postcss-purgecss": "^4.1.3",
"#storybook/addon-actions": "^6.5.0-alpha.51",
"#storybook/addon-essentials": "^6.5.0-alpha.51",
"#storybook/addon-interactions": "^6.5.0-alpha.51",
"#storybook/addon-links": "^6.5.0-alpha.51",
"#storybook/addon-postcss": "^2.0.0",
"#storybook/react": "^6.5.0-alpha.51",
"#storybook/testing-library": "^0.0.9",
"#testing-library/dom": "^7.30.0",
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^10.4.7",
"#testing-library/user-event": "^12.8.3",
"#types/node": "^17.0.21",
"#types/react": "^17.0.40",
"autoprefixer": "^10.4.4",
"babel-loader": "^8.2.3",
"clsx": "^1.1.1",
"enzyme": "^3.11.0",
"eslint": "8.4.1",
"eslint-config-next": "^12.1.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-storybook": "^0.5.7",
"faker": "^5.4.0",
"history": "^5.0.0",
"html-webpack-plugin": "^5.5.0",
"imports-loader": "^2.0.0",
"jest": "^26.6.3",
"jest-axe": "^6.0.0",
"lodash": "^4.17.21",
"postcss": "^8.4.12",
"postcss-import": "^14.1.0",
"storybook-addon-next": "^1.6.2",
"tailwindcss": "^3.0.23",
"typescript": "^4.2.4"
},
There is a config in tailwind called safelisting.
This option said that tailwind classes don't apply when they change in real time and DOM.
read: https://tailwindcss.com/docs/content-configuration#safelisting-classes
you need to add specific classes to safelist to apply for changing in DOM and real-time.
you can add all of tailwind classes to safe list by adding this config to your tailwind.config.js:
module.exports = {
safelist: [
{
pattern: /^(.*?)/,
},
],
// ...
}

How to use CSS modules with parcel?

When I try to do this:
// index.js
import * as classes from './index.module.css'
I get this:
Error: Bundles must have unique names
// .parcelrc
{
"extends": "#parcel/config-webextension"
}
// babel.config.js
module.exports = {
plugins: [
[
"#babel/plugin-proposal-decorators",
{
legacy: true,
},
],
[
"#babel/plugin-proposal-class-properties",
{
loose: true,
},
],
],
presets: [
[
"#babel/preset-env",
{
targets: {
node: "current",
},
loose: true,
},
],
"#babel/preset-react",
],
}
// package.json
{
"private": true,
"scripts": {
"build": "parcel build source/manifest.json --no-content-hash --no-source-maps --dist-dir distribution --no-cache --detailed-report 0",
"lint": "run-p lint:*",
"lint-fix": "run-p 'lint:* -- --fix'",
"lint:css": "stylelint source/**/*.css",
"lint:js": "xo",
"eslint": "eslint source/**/*.js source/**/*.jsx",
"test": "run-p lint:* build",
"watch": "parcel watch source/manifest.json --dist-dir distribution --no-cache --no-hmr"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"source/**/*.{js,jsx}": [
"eslint",
"prettier --write"
],
"source/**/*.css": [
"prettier --write"
]
},
"browserslist": [
"last 1 Chrome version",
"last 1 Firefox version"
],
"xo": {
"envs": [
"browser"
],
"rules": {
"no-unused-vars": [
"error",
{
"varsIgnorePattern": "browser"
}
]
}
},
"stylelint": {
"extends": "stylelint-config-xo"
},
"dependencies": {
"#reduxjs/toolkit": "^1.8.0",
"bootstrap": "^5.1.3",
"react": "^17.0.2",
"react-bootstrap": "^2.2.1",
"react-bootstrap-typeahead": "^6.0.0-alpha.9",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-toastify": "^8.2.0",
"redux-saga": "^1.1.3",
"webext-options-sync": "^3.0.1",
"webextension-polyfill": "^0.8.0"
},
"devDependencies": {
"#babel/core": "^7.17.7",
"#babel/eslint-parser": "^7.17.0",
"#babel/plugin-proposal-class-properties": "^7.16.7",
"#babel/plugin-proposal-decorators": "^7.17.2",
"#babel/preset-env": "^7.16.11",
"#babel/preset-react": "^7.16.7",
"#parcel/config-webextension": "^2.3.2",
"#parcel/transformer-image": "^2.3.2",
"axios": "^0.26.1",
"eslint": "^8.11.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-react": "^7.29.3",
"husky": "4",
"lint-staged": "^12.3.7",
"npm-run-all": "^4.1.5",
"parcel": "^2.3.2",
"postcss": "^8.0.0",
"postcss-modules": "^4.3.0",
"prettier": "^2.5.1",
"process": "^0.11.10",
"rollup-plugin-import-css": "^3.0.3",
"stylelint": "^14.5.3",
"stylelint-config-xo": "^0.20.1",
"xo": "^0.48.0"
},
"webExt": {
"sourceDir": "distribution"
}
}
Error stacktrace
Error: Bundles must have unique names
AssertionError [ERR_ASSERTION]: Bundles must have unique names
at BundlerRunner.nameBundles
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/requests/BundleGraphRequest.js:343:23)
at async BundlerRunner.bundle
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/requests/BundleGraphRequest.js:286:5)
at async RequestTracker.runRequest
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/RequestTracker.js:725:20)
at async Object.run
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/requests/ParcelBuildRequest.js:62:7)
at async RequestTracker.runRequest
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/RequestTracker.js:725:20)
at async Parcel._build
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/Parcel.js:397:11)
at async Parcel._startNextBuild
(/Users/Goldy/apps/web-extension/node_modules/#parcel/core/lib/Parcel.js:298:24)
at async $b0fd219fea43bcac$export$2e2bcd8739ae039._runFn
(/Users/Goldy/apps/web-extension/node_modules/#parcel/utils/lib/index.js:32645:13)
at async $b0fd219fea43bcac$export$2e2bcd8739ae039._next
(/Users/Goldy/apps/web-extension/node_modules/#parcel/utils/lib/index.js:32638:9)
What am I missing here?
I also tried to add the following file with no effect:
// .postcssrc.js
module.exports = {
modules: true,
plugins: {
"postcss-modules": {
generateScopedName: "[folder]__[local]___[hash:base64:6]",
},
},
}

React app with Typescript, Using generic spread expressions throw Unexpected token when trying to run the app

I'm using Typescript with react and i'm trying to create a component using Generic in tsx.
When I created the component, my IDE didn't complain about the syntax and everything seemed to be working properly, but then when I try to run the app, Typescript compiler throw an exception in the console telling me that the syntax is not supported so I assume that the problem is actually coming from by babel configuration.
I'm not sure if it's babel or webpack
I tried different solution available in the internet but none of them solved the issue so far.
My component:
function AutoComplete<T extends { id?: number | string, [key: string]: string | number | any }>(
{
...others
}: Props<T>
) {
return (
<div> My component</div>
)
}
How I use generic in JSX
<AutoComplete
<IContact>
onSearch={handleSearch}
dataSource={contacts}/>
My tsconfig.json
{
"compilerOptions": {
"baseUrl": "src",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"lib": [
"esnext",
"dom",
"scripthost"
],
"jsx": "react",
"declaration": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true
},
"include": [
"./src/**/*"
]
}
My eslintrc
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"modules": true,
"arrowFunctions": true,
"classes": true
},
"env": {
"es6": true,
"browser": true,
"node": true
}
},
"parser": "babel-eslint",
"plugins": [
"react",
"react-hooks",
"#typescript-eslint"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
My package.json
{
"name": "supplier-dashboard-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#ant-design/compatible": "^1.0.2",
"#ant-design/icons": "^4.0.5",
"#bugsnag/js": "6.4.3",
"#bugsnag/plugin-react": "6.4.0",
"#svgr/webpack": "4.3.3",
"#types/node": "12.12.11",
"#types/react": "16.9.29",
"#types/react-dom": "16.9.4",
"#types/react-redux": "7.1.5",
"#types/react-router": "5.1.3",
"#types/react-router-dom": "5.1.2",
"#types/recharts": "^1.8.5",
"antd": "4.1.0",
"axios": "0.19.0",
"camelcase": "5.3.1",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"convert-vapid-public-key": "^1.0.1",
"dotenv": "6.2.0",
"dotenv-expand": "5.1.0",
"file-loader": "3.0.1",
"fs-extra": "7.0.1",
"html-webpack-plugin": "4.0.0-beta.5",
"identity-obj-proxy": "3.0.0",
"is-wsl": "1.1.0",
"lodash-es": "^4.17.15",
"mini-css-extract-plugin": "0.8.0",
"node-sass": "4.13.0",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-normalize": "7.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "4.0.1",
"react": "16.12.0",
"react-app-polyfill": "1.0.4",
"react-currency-input": "^1.3.6",
"react-dev-utils": "9.1.0",
"react-device-detect": "1.9.10",
"react-dom": "16.12.0",
"react-intl": "3.7.0",
"react-redux": "7.1.3",
"react-router": "5.1.2",
"react-router-dom": "5.1.2",
"recharts": "^1.8.5",
"redux": "4.0.4",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "2.3.0",
"resolve": "1.12.2",
"resolve-url-loader": "3.1.1",
"sass-loader": "7.2.0",
"semver": "6.3.0",
"style-loader": "1.0.0",
"terser-webpack-plugin": "1.4.1",
"ts-pnp": "1.1.4",
"url-loader": "2.1.0",
"webpack": "4.41.2",
"webpack-manifest-plugin": "2.2.0",
"workbox-webpack-plugin": "4.3.1"
},
"resolutions": {
"#types/react": "16.9.29"
},
"size-limit": [
{
"limit": "700 KB",
"path": "./build/static/js/*.js",
"webpack": true,
"running": false
}
],
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"build:production": "NODE_ENV=production node scripts/build.js",
"build:staging": "NODE_ENV=staging node scripts/build.js",
"build:training": "NODE_ENV=training node scripts/build.js",
"test": "REACT_APP_ENV=test jest --runInBand && eslint .",
"test:watch": "npm test -- --watch",
"dev": "npm run start",
"size": "npm run build:production && npx size-limit",
"analyze": "source-map-explorer build/static/js/*.js"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"resolver": "jest-pnp-resolver",
"setupFiles": [
"react-app-polyfill/jsdom",
"core-js"
],
"setupFilesAfterEnv": [
"<rootDir>/config/jest/setupTest.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"modulePaths": [
"src"
],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"browser": true
},
"babel": {
"presets": [
[
"#babel/preset-env"
],
"react-app"
],
"plugins": [
"#babel/plugin-proposal-optional-chaining"
]
},
"devDependencies": {
"#babel/core": "7.7.2",
"#babel/plugin-proposal-optional-chaining": "7.6.0",
"#babel/plugin-transform-typescript": "^7.9.4",
"#size-limit/preset-app": "^3.0.0",
"#size-limit/webpack": "^3.0.0",
"#testing-library/jest-dom": "4.2.4",
"#testing-library/react": "9.3.2",
"#types/jest": "^24.0.23",
"#types/lodash-es": "^4.17.3",
"#types/moxios": "^0.4.9",
"#types/redux-mock-store": "^1.0.2",
"#typescript-eslint/eslint-plugin": "2.8.0",
"#typescript-eslint/parser": "2.8.0",
"babel-eslint": "10.0.3",
"babel-jest": "^24.9.0",
"babel-loader": "8.0.6",
"babel-plugin-import": "1.13.0",
"babel-plugin-named-asset-import": "0.3.4",
"babel-preset-react-app": "9.0.2",
"css-loader": "2.1.1",
"eslint": "6.6.0",
"eslint-loader": "3.0.2",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.16.0",
"eslint-plugin-react-hooks": "1.7.0",
"jest": "^24.9.0",
"jest-environment-jsdom-fourteen": "0.1.0",
"jest-resolve": "24.9.0",
"jest-watch-typeahead": "0.4.2",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"moxios": "^0.4.0",
"redux-mock-store": "^1.5.3",
"ts-jest": "^25.1.0",
"typescript": "^3.7.2",
"webpack-dev-server": "3.2.1"
}
}
You should use typescript eslint parser:
// eslintrc.json
...
"parser": "#typescript-eslint/parser",
...
And also extends typescript babel preset. More about presets
// package.json
...
"babel": {
"presets": [
// you can use inline syntax if you don't need to pass config options
"#babel/preset-env",
// Add this preset
"#babel/preset-typescript",
"react-app"
],
"plugins": [
"#babel/plugin-proposal-optional-chaining"
]
},
...
It should do the trick :)

react redux Parsing error: Unexpected character '​'

Why this error appear?
package.json
{
"name": "react-shopping",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/core": "7.1.0",
"#svgr/webpack": "2.4.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-named-asset-import": "^0.2.3",
"babel-preset-react-app": "^6.1.0",
"bfj": "6.1.1",
"case-sensitive-paths-webpack-plugin": "2.1.2",
"chalk": "2.4.1",
"css-loader": "1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.6.0",
"eslint-config-react-app": "^3.0.5",
"eslint-loader": "2.1.1",
"eslint-plugin-flowtype": "2.50.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.2",
"eslint-plugin-react": "7.11.1",
"file-loader": "2.0.0",
"fork-ts-checker-webpack-plugin-alt": "0.4.14",
"fs-extra": "7.0.0",
"html-webpack-plugin": "4.0.0-alpha.2",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"mini-css-extract-plugin": "0.4.3",
"optimize-css-assets-webpack-plugin": "5.0.1",
"pnp-webpack-plugin": "1.1.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-preset-env": "6.0.6",
"postcss-safe-parser": "4.0.1",
"react": "^16.6.1",
"react-app-polyfill": "^0.1.3",
"react-dev-utils": "^6.1.1",
"react-dom": "^16.6.1",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"resolve": "1.8.1",
"sass-loader": "7.1.0",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "3.1.9",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.3"
},
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"resolver": "jest-pnp-resolver",
"setupFiles": [
"react-app-polyfill/jsdom"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
],
"testEnvironment": "jsdom",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
]
},
"babel": {
"presets": [
"react-app"
]
}
}
store.js
import {createStore} from 'redux';
function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([action.text]);
default:
return state;
}
}
const store = createStore(todos, ['Use Redux']);
​
store.dispatch({
type: 'ADD_TODO',
text: 'Read the docs'
});
​
console.log(store.getState());
App.js
import React, {Component} from 'react';
import './store';
class App extends Component {
render() {
return (
<div className="App">
<h1>asdas</h1>
</div>
);
}
}
export default App;
Seems to me like a lint bug. When I removed two blank lines, I didn't get any errors:
import { createStore } from 'redux';
function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([action.text]);
default:
return state;
}
}
const store = createStore(todos, ['Use Redux']);
store.dispatch({
type: 'ADD_TODO',
text: 'Read the docs'
});
console.log(store.getState());

babel 7 Using the export keyword between a decorator and a class is not allowed. Please use `export #dec class` instead

I'm using react js with latest babel 7.
when I use decorators of mobx I get the error
Parsing error: Using the export keyword between a decorator and a class is not allowed. Please use export #dec class instead.
my code
import React, { Component } from 'react';
import { observer,inject } from 'mobx-react'
import './style.scss'
#inject('routingStore', 'UserStore')
#observer
export default class Login extends Component {
constructor(props) {
super(props)
this.state = {
}
}
render() {
return (
<div>
<h1>Login</h1>
</div>
)
}
}
package.json
{
"name": "admin-managment-barber",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/core": "7.1.0",
"#svgr/webpack": "2.4.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-named-asset-import": "^0.2.3",
"babel-preset-react-app": "^6.1.0",
"bfj": "6.1.1",
"case-sensitive-paths-webpack-plugin": "2.1.2",
"chalk": "2.4.1",
"css-loader": "1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.6.0",
"eslint-config-react-app": "^3.0.5",
"eslint-loader": "2.1.1",
"eslint-plugin-flowtype": "2.50.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.2",
"eslint-plugin-react": "7.11.1",
"file-loader": "2.0.0",
"fork-ts-checker-webpack-plugin-alt": "0.4.14",
"fs-extra": "7.0.0",
"html-webpack-plugin": "4.0.0-alpha.2",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"mini-css-extract-plugin": "0.4.3",
"mobx": "^5.6.0",
"mobx-react": "^5.4.2",
"mobx-react-router": "^4.0.5",
"moment": "^2.22.2",
"optimize-css-assets-webpack-plugin": "5.0.1",
"pnp-webpack-plugin": "1.1.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-preset-env": "6.0.6",
"postcss-safe-parser": "4.0.1",
"react": "^16.6.3",
"react-app-polyfill": "^0.1.3",
"react-dev-utils": "^6.1.1",
"react-dom": "^16.6.3",
"react-router-dom": "^4.3.1",
"resolve": "1.8.1",
"sass-loader": "7.1.0",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "3.1.9",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.3"
},
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"resolver": "jest-pnp-resolver",
"setupFiles": [
"react-app-polyfill/jsdom"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
],
"testEnvironment": "jsdom",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
]
},
"babel": {
"plugins": [
[
"#babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"#babel/plugin-proposal-class-properties",
{
"loose": true
}
]
],
"presets": [
"react-app"
]
},
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.1.0",
"#babel/plugin-proposal-decorators": "^7.1.6",
"#babel/plugin-transform-react-jsx-source": "^7.0.0"
}
}
i guess it's maybe babel configuration or something. I stuck with this issue for some hours and I also tried to downgrade babel to 6.23.0 but without success.
Instead of changing the import statements across your project, you could use the legacyDecorators option in your .eslintrc file.
{
"parserOptions": {
"ecmaFeatures": {
"legacyDecorators": true
}
}
}
There is a work around you can do. Modify your class to the following.
import React, { Component } from 'react';
import { observer,inject } from 'mobx-react'
import './style.scss'
#inject('routingStore', 'UserStore')
#observer
class Login extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<h1>Login</h1>
</div>
);
}
}
export default Login;
Technically, you just move export to the bottom of the class. If you have a lot of classes, that solution is not the best one. I couldn't find better way, yet.
This worked for me
(using create-react-app with customize-cra, with decorators enabled)
import React, { Component } from 'react';
export default #connect(
store => ({
//...
}),
dispatch => ({
//...
}),
)
class MyComponent extends Component {
render() {
return (
<div>
//...
</div>
);
}
}

Resources