I'm new to using React and Redux, I'm building a simple todos application. I am writing my application using the create-react-app tool in the command line.
The issue
I went to other blogs and post and others have mentioned that I am missing an npm plugin to transform decorators "transform-decorators-legacy", I added this to my dependencies along with Babel, but I am still receiving the same error.
Syntax error: Unexpected token (9:0)
7 | import './App.css';
8 |
> 9 | #connect((store) => {
| ^
10 | return {
11 | user: store.user.user
12 | }
My code
import React, { Component } from 'react';
import { connect } from 'react-redux';
import Todos from './components/Todos';
import Todo from './components/Todo';
import './App.css';
#connect((store) => {
return {
user: store.user.user
}
})
class App extends Component {
constructor(){
super()
this.state = {
name: 'Brad',
todos: [
{
id: '001',
name: 'Take out trash',
completed: false
},
{
id: '002',
name: 'Meeting with boss',
completed: false
},
{
id: '003',
name: 'Go out to dinner',
completed: false
}
]
}
}
render() {
return (
<div className="App">
<h1>Hello</h1>
<Todos name={this.state.name} todos={this.state.todos}/>
<Todo/>
</div>
);
}
}
export default App;
My dependencies
package.json
{
"name": "react-redux-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.2",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^4.3.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"react-scripts": "1.0.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Any help/knowledge will be appreciated thanks!
First I'd note that I agree with #markerikson's answer, use the function instead of the decorator.
If you did want to use decorators though, there would be a couple more steps involved. You've added the plugin to your dependencies, but not told Babel to use it.
Create React App uses Webpack with Babel under the hood, namely, in react-scripts. You need to adjust that Babel configuration. The "Create React App" way to do this is to eject from Create React App and then edit .babelrc - see https://babeljs.io/docs/plugins/transform-decorators/#usage-via-babelrc-recommended-
Please note that both the React and Redux teams generally discourage the use of decorators. Instead, in this case you should use connect() as a function:
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
Try like this:
const stateMap = (state) => {
console.log('state', state);
return {
//something from state
};
};
export default connect(stateMap)(App);
Related
I've just created a new React application in Visual Studio 2017 (not VSCode as a lot of the other posts on this error seem to be using) and it's set the default file contents of:
package.json
{
"name": "React",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^3.3.7",
"react": "^16.0.0",
"react-bootstrap": "^0.31.5",
"react-dom": "^16.0.0",
"react-router-bootstrap": "^0.24.4",
"react-router-dom": "^4.2.2",
"react-scripts": "1.0.17",
"rimraf": "^2.6.2"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
App.js
import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
export default class App extends Component {
displayName = App.name
render() {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route path='/counter' component={Counter} />
<Route path='/fetchdata' component={FetchData} />
</Layout>
);
}
}
I'm trying to follow a tutorial to add a new component but when I add the following component it complains with an error message of "(JS) 'type assertion expressions' can only be used in a .ts file":
I've found quite a few other posts which say to add the following code but I've added this to the appsettings.json file and that hasn't had any effect:
"javascript.validate.enable": false
Most posts around this subject seem to be using VSCode as well so not sure if this makes a difference that I'm on VS2017? Any ideas on what I need to alter to allow this to compile and run?
I am trying to get data from firebase collection but getting error, can anybody help?
I am getting this error
Error: Context from react-redux not found. If you are using react-redux v6 a v3.. version of react-redux-firebase is required. Please checkout the v3 migration guide:
here is my code.
import React, { Component } from 'react'
import Notifications from './Notifications';
import ProjectList from '../projects/ProjectList';
import {connect} from 'react-redux';
import {firestoreConnect} from 'react-redux-firebase';
import {compose} from 'redux';
import { firebaseConnect, isLoaded, isEmpty } from 'react-redux-firebase'
class Dashboard extends Component {
render() {
return (
<div className="dashboard container">
<div className="row">
<div className="col s12 m6"><ProjectList projects={this.props.projects}></ProjectList></div>
<div className="col s12 m5 offset-m1"><Notifications></Notifications></div>
</div>
</div>
)
}
}
const mapStateToProps = (state) =>
{
console.log(state);
return ({projects:state.projectReducer.projects})
}
export default compose(firebaseConnect([
'projects'
]), connect(mapStateToProps)) (Dashboard)
I have tried this export but same error
export default compose(connect(mapStateToProps), firestoreConnect([
{collection:'project'}
])) (Dashboard)
here is my package.json
{
"name": "myproject",
"version": "0.1.0",
"private": true,
"dependencies": {
"firebase": "^5.7.0",
"firedux": "^1.1.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-redux": "^6.0.0",
"react-redux-firebase": "^2.2.5",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"redux": "^4.0.1",
"redux-firestore": "^0.6.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
The error is quite clear, you are using react-redux-firebase:^2.2.5 while you need to use v3, because you are using react-redux: ^6.0.0.
You have to update react-redux-firebase
If you're following along with someone's example and not building a production app, you can just switch to react-redux 5.1.1.
Just change "react-redux": "^6.0.0", to "react-redux": "^5.1.1", in package.json and run npm install.
#kabanny above is quite correct.
If you really want to use the next version of react-redux-firebase that supports the context api, then do the following at your own risk.
npm install react-redux-firebase#next
Also check out the migration guide
If you did install the packages using npm. downgrading to react-redux-firebase#2.2.5 and react-redux#5.1.1 did solve the problem in my case. Something to take note of: In case you face such errors when following someone's tutorials, I recommend you first confirm the version of dependencies they are using. npm can land you in trouble.
I'm new to using React and Redux, I'm building a simple todos application. I am writing my application using the create-react-app tool in the command line.
The issue
I went to other blogs and post and others have mentioned that I am missing an npm plugin to transform decorators "transform-decorators-legacy", I added this to my dependencies along with Babel, but I am still receiving the same error.
Syntax error: Unexpected token (9:0)
7 | import './App.css';
8 |
> 9 | #connect((store) => {
| ^
10 | return {
11 | user: store.user.user
12 | }
My code
import React, { Component } from 'react';
import { connect } from 'react-redux';
import Todos from './components/Todos';
import Todo from './components/Todo';
import './App.css';
#connect((store) => {
return {
user: store.user.user
}
})
class App extends Component {
constructor(){
super()
this.state = {
name: 'Brad',
todos: [
{
id: '001',
name: 'Take out trash',
completed: false
},
{
id: '002',
name: 'Meeting with boss',
completed: false
},
{
id: '003',
name: 'Go out to dinner',
completed: false
}
]
}
}
render() {
return (
<div className="App">
<h1>Hello</h1>
<Todos name={this.state.name} todos={this.state.todos}/>
<Todo/>
</div>
);
}
}
export default App;
My dependencies
package.json
{
"name": "react-redux-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.2",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^4.3.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"react-scripts": "1.0.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Any help/knowledge will be appreciated thanks!
First I'd note that I agree with #markerikson's answer, use the function instead of the decorator.
If you did want to use decorators though, there would be a couple more steps involved. You've added the plugin to your dependencies, but not told Babel to use it.
Create React App uses Webpack with Babel under the hood, namely, in react-scripts. You need to adjust that Babel configuration. The "Create React App" way to do this is to eject from Create React App and then edit .babelrc - see https://babeljs.io/docs/plugins/transform-decorators/#usage-via-babelrc-recommended-
Please note that both the React and Redux teams generally discourage the use of decorators. Instead, in this case you should use connect() as a function:
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
Try like this:
const stateMap = (state) => {
console.log('state', state);
return {
//something from state
};
};
export default connect(stateMap)(App);
Hi I am creating an SPFX weather webpart and i am getting this error:
there are no errors when i run gulp build. i am not sure how to debug my issue. this is the snippet of the proptypes.shape() where i am getting my issue:
import * as React from 'react';
import PropTypes from 'prop-types';
export const Day: React.SFC<any> = props => {
const date = props.day.dt;
const icon = getIcon(props.day.weather[0].id);
const animate = true;
const iconSize = 64;
const iconColor = 'black';
return (
<div className={appClasses.dayContainer} onClick={props.onClick} role="link">
<h2 className={appClasses.date}>{(new Date(date * 1000)).toDateString()} - {(new Date(date * 1000)).toLocaleTimeString()}</h2>
<ReactAnimatedWeather
icon={icon}
color={iconColor}
size={iconSize}
animate={animate}
/>
</div>
);
};
Day.defaultProps = {
onClick: () => {},
};
Day.propTypes = {
day: PropTypes.shape({
dt: PropTypes.number.isRequired,
weather: PropTypes.array.isRequired,
}).isRequired,
onClick: PropTypes.func,
};
I'd like to note that i created the webpart first using react and it is working perfectly, but when i created an SPFX app, and transferred my existing codes into it. I had encountered these errors.
This is my package.json
{
"name": "spfx-weather-2",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"#microsoft/sp-core-library": "~1.1.0",
"#microsoft/sp-webpart-base": "~1.1.1",
"#types/react": "0.14.46",
"#types/react-addons-shallow-compare": "0.14.17",
"#types/react-addons-test-utils": "0.14.15",
"#types/react-addons-update": "0.14.14",
"#types/react-dom": "0.14.18",
"#types/webpack-env": ">=1.12.1 <1.14.0",
"prop-types": "^15.6.1",
"react": "15.4.2",
"react-animated-weather": "^1.0.3",
"react-dom": "15.4.2",
"react-router-dom": "^4.2.2"
},
"devDependencies": {
"#microsoft/sp-build-web": "~1.1.0",
"#microsoft/sp-module-interfaces": "~1.1.0",
"#microsoft/sp-webpart-workbench": "~1.1.0",
"gulp": "~3.9.1",
"#types/chai": ">=3.4.34 <3.6.0",
"#types/mocha": ">=2.2.33 <2.6.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
}
}
Check my answer here [SPLoaderError.loadComponentError]: ***Failed to load component
You very likely have the same issue. If you were to post your github link for me to clone I could confirm for you.
TL;DR
You likely have a circular reference between your factories. You need to move any code that is required from your top level factory to the bottom of said factory.
Let me know if you don't quite understand after reading my other answer.
I'm building a React/Redux/ReactRouter/Jest boilerplate but I'm having an issue when testing a component with react-test-renderer.
I have put in place two kind of tests: unit tests for my redux actions and snapshot tests as a form of functional tests for my components.
First of all, my unit tests work perfectly. Here's one:
import mockStore from 'redux-mock-store';
import {fetchWordDefinitions} from '../src/actions';
describe('Async fetch of word definitions', () => {
it('fetches a word definitions', async () => {
const mockedResponse = [
{text: 'First definition'},
{text: 'Second definition'},
{text: 'Third definition'}
];
fetch.mockResponse(JSON.stringify(mockedResponse));
const store = mockStore({});
await store.dispatch(fetchWordDefinitions('whatever'));
expect(store.getActions()).toEqual([
{type: 'ERROR', error: null},
{type: 'FETCHING', fetching: true},
{
type: 'WORD_DEFINITIONS',
word: 'whatever',
definitions: [
"First definition",
"Second definition",
"Third definition"
]
},
{type: 'FETCHING', fetching: false}
]);
});
});
As you can see I'm using ES6 there (both in the test and in the tested action) and everything works fine.
The problem is when I try to test a component by creating it with react-test-renderer. Here's the broken test:
import React from 'react';
import {Provider} from 'react-redux';
import renderer from 'react-test-renderer';
import mockStore from 'redux-mock-store';
import Home from './../src/containers/Home';
test('test', () => {
const store = mockStore({});
const container = renderer.create(
<Provider store={store}>
<Home/>
</Provider>
);
// some assertions - execution does not get here
});
Here's what I get in the CLI:
FAIL __tests__/Home.test.js
● Test suite failed to run
/data/src/containers/Home.js: Unexpected token (8:11)
6 |
7 | class Home extends ReduxComponent {
> 8 | search = (value) => {
| ^
9 | if (value !== this.status().selectedWord) {
10 | this.dispatch(fetchRelatedWords(value));
11 | }
And this is my .babelrc file (which is sitting in the root of my project folder):
{"presets": ["es2015", "react"]}
And finally the packages.json file:
{
"name": "vocabulary",
"version": "0.1.0",
"author": "Francesco Casula <fra.casula#gmail.com>",
"license": "MIT",
"private": false,
"dependencies": {
"prop-types": "^15.5.8",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"redux": "^3.6.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-jest": "^19.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"jest-fetch-mock": "^1.0.8",
"react-scripts": "^0.9.5",
"react-test-renderer": "^15.5.4",
"redux-logger": "^3.0.1",
"redux-mock-store": "^1.2.3",
"regenerator-runtime": "^0.10.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "jest"
},
"jest": {
"verbose": true,
"setupFiles": [
"./config/jest/setup.js"
]
}
}
By looking at the error it seems like babel may not be doing its magic?
What I find weird though is that is transpiling correctly in the other tests (the action ones).
Hope you guys can help me figure this out :-)
You will need the class-properties transform. Class properties are not yet in the ECMAScript spec but there are Babel plugins to allow this behavior.