React reading from firebase, getting error - reactjs

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.

Related

Getting invalid hook call warning when using react-bootstrap

I'm having issues with react-bootstrap, specifically whenever I try and use a react-bootstrap tag, like <Row></Row>, I'm getting the "Invalid hook call" error on my app.
I've run npm ls and these are the packages/versions I currently have installed:
boostrap#4.6.0
react-bootstrap#1.6.1
react-dom#17.0.2
react#17.0.2
I've also followed the advice in the React documentation on this error and added logs to see if I've got duplicate copies of React, but the test is returning 'true' in the console.
Here's the package.json file in my project:
{
"name": "kanvaswebappr",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.1.3",
"jquery": "^3.5.1",
"merge": "^1.2.1",
"oidc-client": "^1.9.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.4",
"reactstrap": "^8.4.1",
"rimraf": "^2.6.2"
},
"devDependencies": {
"ajv": "^6.9.1",
"cross-env": "^5.2.0",
"typescript": "^3.7.5",
"eslint": "^6.8.0",
"eslint-config-react-app": "^5.2.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.3",
"nan": "^2.14.1"
},
"peerDependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"eslintConfig": {
"extends": "react-app"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "cross-env CI=true react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"lint": "eslint ./src/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
As you can see, I've moved react and reactdom into peeDependencies and then run npm install, but that hasn't helped either.
My app is currently super basic because I'm just starting out/learning.
My index.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './containers/App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const rootElement = document.getElementById('root');
ReactDOM.render(
<BrowserRouter basename={baseUrl}>
<App />
</BrowserRouter>,
rootElement);
My App.js file:
import React, { Component } from 'react';
import './App.css';
import Layout from '../components/Layout/Layout';
class App extends Component {
render() {
return (
<Layout>
<strong>This content is going to be rendered as the props.children inside the Layout component.</strong>
</Layout>
);
}
}
export default App;
My Layout.js file:
import React from 'react';
import Alert from 'react-bootstrap/Alert';
const layout = (props) => {
return (
<div>
<div>
This is the place for the navigation component.
</div>
<Alert>
This is an alert—check it out!
</Alert>
<main>
{props.children}
</main>
</div>
)
}
export default layout;
If anyone is able to help I would be incredibly grateful!
I fixed it myself after a bit more messing around this morning. Posting this here in case it's of any use to anyone else.
I'm building my React app as part of an asp.net solution and I had a node_modules folder at both solution level and in my actual React app/project. I'm very new to all this so I think I'd managed to install packages in both places and they were out of sync in terms of what was installed and version numbers. I ran npm update at both levels to bring everything up to date and then I ran npm link MyApp/node-modules/react from the solution folder just to make sure that React wasn't being duplicated (although I don't think it was).
All seems to be working now.
solved it by going into node modules folder to check the versions react and react-dom installations. then I changed those that were behind. actually i deleted the node modules folder.

Can't use Material-UI on React app inside a Visual Studio C# ASP. NET Core Web Application

I had created a new .NET Core web application with the React and Redux template. Everything is working fine with the exception that when I install the Material-UI library in my ClientApp folder (the one who has the react project) and import a component from the library, the app throws the next error:
TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function
My package.json looks like this:
{
"name": "Evento",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.11.0",
"bootstrap": "^3.4.1",
"react": "^16.0.0",
"react-bootstrap": "^0.31.5",
"react-dom": "^16.0.0",
"react-redux": "^5.0.6",
"react-router-bootstrap": "^0.24.4",
"react-router-dom": "^4.2.2",
"react-router-redux": "^5.0.0-alpha.8",
"react-scripts": "1.0.17",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"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"
}
}
And the component from which I want to import a Material UI Button looks like this:
import React, { Component } from 'react';
import { Button } from '#material-ui/core';
export default class Main extends Component {
render() {
return (
<div>Hello world!</div>
);
}
}
Why is this happening and what can I do to solve it?
You need to update React. v16.0.0 is too old for Material UI 4.11. You should update React and React DOM to at least 16.8.0:
npm update react react-dom
Make sure you restart your app afterward so Webpack can rebundle the new version.

An error message "'Office' is not defined no-undef" happens if react-script is newer than 3.0.0

I am creating outlook add-in with react/typescript. When I run my code with npm run start, following message appeared and I was not able to run my react app.
Failed to compile.
./src/index.tsx
Line 9: 'Office' is not defined no-undef
Search for the keywords to learn more about each error.
In package.json, if react-script version is newer than 3.0.0, This error happens. If it is lower(e.x. 2.8.8), we can execute the react app without errors. Does anyone give me some workarounds?
Index.tsx
import 'react-app-polyfill/ie11';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { AppContainer } from './components';
import { Provider } from 'react-redux';
import { store } from './store/store';
Office.onReady(() => {
ReactDOM.render(
<Provider store={store}>
<AppContainer />
</Provider>,
document.getElementById('root')
);
});
package.json
"name": "outlookApp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#microsoft/office-js": "^1.1.29",
"#microsoft/office-js-helpers": "^1.0.2",
"#types/jest": "24.0.11",
"#types/node": "11.13.6",
"#types/react": "16.8.23",
"#types/react-dom": "16.8.4",
"office-ui-fabric-react": "^6.190.1",
"react": "^16.8.6",
"react-app-polyfill": "^1.0.1",
"react-dom": "^16.8.6",
"react-redux": "^7.1.0",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.0",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0",
"typescript": "3.4.4",
"uuid": "^3.3.2"
},
"devDependencies": {
"#types/gtag.js": "0.0.0",
"#types/office-js": "1.0.1",
"#types/react-redux": "^7.1.0",
"#types/react-router-dom": "^4.3.4",
"#types/uuid": "^3.4.5",
"redux-devtools-extension": "^2.13.8"
},
"scripts": {
"start": "react-scripts start",
"win": "set HTTPS=true&&set BROWSER=none&&react-scripts start",
"mac": "export HTTPS=true&&export BROWSER=none&&react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test-win-ci": "set CI=true&&npm test",
"test-mac-ci": "CI=true npm test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"proxy": "http://localhost:7071"
}
One workaround is to just put the comment "/* global Excel, Office */" into the JS/TS/TSX file that it complains about, and things will magically work.
Note: The solution is taken from a comment at https://github.com/OfficeDev/office-js-docs-pr/issues/691. If anyone knowns of a better solution, please share it with the community!

Module not found: Can't resolve 'redux' in node_modules

When I used typescript to create my react project, I introduced Redux error reporting, which indicated that I could not find it, but I had to install all the dependencies.
Here's my package.json
{
"name": "react-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"#types/jest": "^24.0.11",
"#types/node": "^11.11.3",
"#types/react": "^16.8.8",
"#types/react-dom": "^16.8.2",
"#types/react-redux": "^7.0.3",
"#types/react-router-dom": "^4.3.1",
"axios": "^0.18.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-redux": "^6.0.1",
"react-router-dom": "^4.4.0",
"react-scripts": "2.1.8",
"redux": "^4.0.1",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
"typescript": "^3.3.3333"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"babel-plugin-import": "^1.11.0",
"customize-cra": "^0.2.12",
"node-sass": "^4.11.0",
"react-app-rewired": "^2.1.1"
}
}
Here's my store
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import reducer from './reducer'
const store = createStore(reducer, composeWithDevTools(
applyMiddleware(thunk)
))
Unfortunately, the app doesn't compile and it complains with:
./node_modules/_react-redux#6.0.1#react-redux/es/connect/mapDispatchToProps.js
Module not found: Can't resolve 'redux' in '~/node_modules/_react-redux#6.0.1#react-redux/es/connect'
What should I do is right. Please help me, thanks.
Try to install both redux and react-redux at same time:
npm install --save redux react-redux
This worked for me
npm install --save redux react-redux
For people that might come across this in the future, I came across the same issue.
I was calling connect(mapStateToProps, null)(Component). All I had to do was actually pass in a proper mapDispatchToProps argument and it solved the problem.
It had nothing to do with redux not being properly installed.
--save means that dependency install as local dependency, that means related for that particular project.
npm install redux -g in here redux install as global dependency
This works for me in typescript as follows:
yarn add #types/redux #types/react-redux
(or using npm try: npm install add #types/redux #types/react-redux)
You my find it stupid but for my case it was: I'm working with both node project and react project opened in a single VSCODE window so i was added those redux packages into the Wrong package.json file :(

How do I properly set up my Babel plugins and presets to make es6 decorators work in my React app?

I have a React app going, and I'm trying to get the hang of decorators. They're causing unexpected token errors.
I'm using the transform-decorators-legacy and transform-class-properties babel plugins.
I've tried both the stage-0 and stage-2 presets. All the related posts on SO and elsewhere seem to think that the plugins should make it work. Some say to use the stage-0 preset, while others recommend stage-2.
Here's the file with the decorator:
import React, {Component} from 'react';
import Actions from '../firebase/actions';
import RecipeList from '../Recipe/recipe-list';
import connectToStores from 'alt-utils/lib/connectToStores';
import RecipeStore from '../../store/recipe-store';
#connectToStores
class Main extends Component {
constructor() {
super();
Actions.getRecipes();
}
static getStores() {
return [RecipeStore];
}
static getPropsFromStores() {
return RecipeStore.getState();
}
render() {
return (
<section>
<section className="container">
{
this.props.recipes
?
<RecipeList recipeList={this.props.recipes}/>
:
null
}
</section>
</section>
);
}
}
export default Main;
Here's the package.json file:
{
"name": "glutenfreehacker",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"firebase": "^4.12.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"recompose": "^0.26.0",
"redux": "^4.0.0"
},
"babel": {
"presets": [
"env",
"stage-2",
"react"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I understand this has been discussed here and elsewhere. However, the solutions I've seen suggest using the above plugins and presets, and I still can't get it to work.
Thanks for reading, and any help is greatly appreciated.
For decorators, use the babel-plugin-transform-decorators.
Installation
npm install --save-dev babel-plugin-transform-decorators
Usage
Via .babelrc:
{
"plugins": ["transform-decorators"]
}

Resources