React-Redux: Cannot read property 'closed' of undefined - reactjs

I seem to have run into an issue when using dispatch() with React-Redux. For example, the following action:
export const fetchMetrics = () => {
dispatch(fetchMetricsBegin);
APIService.get('/dashboard/info/')
.then((response) => {
dispatch(fetchMetricsSuccess(response));
return response;
})
.catch(error => dispatch(fetchMetricsFailure(error)));
};
Produces the following error:
TypeError: Cannot read property 'closed' of undefineddispatch
src/internal/observable/pairs.ts:82
Here's my package.json:
{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#popperjs/core": "^2.6.0",
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/react": "^11.2.2",
"#testing-library/user-event": "^12.6.0",
"axios": "^0.21.0",
"dotenv": "^8.2.0",
"jwt-decode": "^3.1.2",
"react": "^17.0.1",
"react-datepicker": "^3.3.0",
"react-dom": "^17.0.1",
"react-images-upload": "^1.2.8",
"react-number-format": "^4.4.1",
"react-popper": "^2.2.4",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"rxjs": "^6.6.3",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I have tried purging node_module and package-lock.json, but the issue is still occurring. Would welcome any insight. Removing the dispatch calls removes the error.

you are using redux-thunk. this library will allow you to perform async calls since your function return another function with dispatch as param. otherwise thunk will execute the code sync.
export const fetchMetrics = () => (dispatch) => {
dispatch(fetchMetricsBegin);
APIService.get('/dashboard/info/')
.then((response) => {
dispatch(fetchMetricsSuccess(response));
return response;
})
.catch(error => dispatch(fetchMetricsFailure(error)));
};

Related

showing error that api post is not found in my react website which is uploaded on aws?

package.json
{
"name": "portfolio1",
"version": "0.1.0",
"private": true,
"proxy": "http://65.2.97.16:3000",
"dependencies": {
"#emotion/react": "^11.10.0",
"#emotion/styled": "^11.10.0",
"#fortawesome/fontawesome-svg-core": "^6.1.2",
"#fortawesome/free-regular-svg-icons": "^6.1.2",
"#fortawesome/free-solid-svg-icons": "^6.1.2",
"#fortawesome/react-fontawesome": "^0.2.0",
"#heroicons/react": "^1.0.6",
"#mui/material": "^5.10.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"overlay-navbar": "^1.2.3",
"react": "^18.1.0",
"react-alert": "^7.0.3",
"react-alert-template-basic": "^1.0.2",
"react-dom": "^18.1.0",
"react-helmet": "^6.1.0",
"react-icons": "^4.4.0",
"react-js-pagination": "^3.0.3",
"react-line-awesome": "^1.3.2",
"react-rating-stars-component": "^2.2.0",
"react-redux": "^8.0.2",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-to-print": "^2.14.7",
"redux": "^4.2.0",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "nodemon App.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.4.8",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8"
},
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
"main": "postcss.config.js",
"keywords": [],
"author": "",
"license": "ISC"
}
contactAction.js
import {
CONTACT_REQUEST,
CONTACT_SUCCESS,
CONTACT_FAIL,
CLEAR_ERRORS,
} from "../constants/contactConstant";
import axios from "axios";
///COntact US SEction
export const contactUs = (userData) => async (dispatch) => {
try {
dispatch({ type: CONTACT_REQUEST });
const config = { headers: { "Content-Type": "application/json" } };
const { data } = await axios.post(`/api/contact`, userData, config);
dispatch({ type: CONTACT_SUCCESS, payload: data.contact });
} catch (error) {
dispatch({
type: CONTACT_FAIL,
payload: error.response.data.message,
});
}
};
// Clearing Errors
export const clearErrors = () => async (dispatch) => {
dispatch({ type: CLEAR_ERRORS });
};
our backend is working properly on aws but when we try to fetch data or post data on our react website which is also hosted on aws it showing error that xhr.js:220 "POST https://www.apieromedica.com/api/contact 404 (Not Found)" but when we try it on our system on localhost everthing work please help me.

getting Uncaught (in promise) Error: invalid BigNumber when calling SC method from react

I am getting the following error when trying to call a smart contract method from react app.
index.ts:261 Uncaught (in promise) Error: invalid BigNumber string (argument="value", value="NaN", code=INVALID_ARGUMENT, version=bignumber/5.6.2)
at e.value (index.ts:261:28)
at e.value (index.ts:273:20)
at e.value (index.ts:277:21)
at Function.value (bignumber.ts:238:27)
at Function.value (bignumber.ts:250:30)
at a.value (number.ts:25:27)
at array.ts:71:19
at Array.forEach (<anonymous>)
at x (array.ts:54:12)
at a.value (tuple.ts:54:16)
It is throwing this error before the request even reaches the metamask. The weird thing about this error is it is only happening in a few system and in others it is working fine. We tried using the same metamask account for both systems, for one where it was failing and one where it was not but the same account request goes through fine in one system and in one
it fails.
const requestService = async (selectedId, id, capacity, limit, account) => {
const res = await P2P.methods
.requestService(
parseInt(selectedId),
parseInt(id),
parseInt(capacity),
ethers.utils.parseEther(String(limit))
)
.send({
from: account,
gas: 2000000,
});
return res;
};
This is the method that is giving me errors. I am using the following dependencies:-
{
"name": "name",
"version": "0.1.0",
"private": true,
"dependencies": {
"#hookform/resolvers": "^2.9.3",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"ethers": "^5.6.9",
"http": "^0.0.1-security",
"https": "^1.0.0",
"mdbreact": "^5.2.0",
"metamask-logo": "^2.5.0",
"os": "^0.1.2",
"react": "^18.2.0",
"react-bootstrap": "^2.4.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.33.0",
"react-icons": "^4.4.0",
"react-router-dom": "^6.3.0",
"react-scripts": "4.0.3",
"react-toastify": "9.0.3",
"react-yup": "^1.24.0",
"reactstrap": "^9.1.1",
"stream": "^0.0.2",
"stream-browserify": "^3.0.0",
"url": "^0.11.0",
"web-vitals": "^2.1.4",
"web3": "^1.7.4"
},
"devDependencies": {
"react-error-overlay": "6.0.9"
},
"scripts": {
"start": "react-scripts --max_old_space_size=4096 start",
"build": "react-scripts --max_old_space_size=4096 build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

Typescript React component type mismatch

Getting a typescript error when defining any type of simple react functional component with any number of props, including using FC, ComponentWithChildren etc...
Here's a minimal reproduction
type Props = {
className?: string;
};
const Horizontal: Props = ({ className = undefined }) => (
<div />
);
Gives me the error
Type '({ className }: { className?: undefined; }) => Element' has no properties in common with type 'Props'.ts(2559)
I don't understand this is how i always defined components. Is there something new in typescript that i don't know about?
{
"name": "call-mvp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.2.0",
"#testing-library/user-event": "^13.5.0",
"#types/classnames": "^2.3.1",
"#types/jest": "^27.5.1",
"#types/node": "^16.11.36",
"#types/react": "^18.0.9",
"#types/react-dom": "^18.0.4",
"#types/react-measure": "^2.0.8",
"#types/react-router-dom": "^5.3.3",
"#types/styled-components": "^5.1.25",
"classnames": "^2.3.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-measure": "^2.5.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"typescript": "^4.6.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
you can change it to:
const Horizontal: FC = ({ className = undefined } : Props)
This should work :
const Horizontal = ({ className = undefined } : Props) => ....

React-Bootstrap - CRA - Failed to compile

When I install react-bootstrap in my CRA react app, and try to import any component such as:
import Form from "react-bootstrap";
I get this error:
Failed to compile.
./node_modules/react-bootstrap/esm/SplitButton.js
Module parse failed: Unexpected token (101:2)
You may need an appropriate loader to handle this file type.
| renderMenuOnMount,
| rootCloseEvent,
| ...props
| }, ref) => /*#__PURE__*/_jsxs(Dropdown, {
| ref: ref,
My package.json looks like this:
{
"name": "component-3",
"version": "0.1.1",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.15.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"axios": "^0.23.0",
"bootstrap": "^5.1.3",
"dotenv": "^10.0.0",
"firebase": "^8.10.0",
"firebase-admin": "^9.12.0",
"react": "^17.0.2",
"react-bootstrap": "^2.0.2",
"react-color-extractor": "^1.1.2",
"react-dom": "^17.0.2",
"react-firebase-hooks": "^3.0.4",
"react-google-button": "^0.7.2",
"react-router-dom": "^5.2.0",
"react-scripts": "^1.1.5",
"react-select": "^5.2.1",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "set NODE_ENV=dev && CHOKIDAR_USEPOLLING=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I want to use components such as Form in my app. I have used react-bootstrap with CRA before with no issues.
I have seen similar posts but none with this exact issue.
Any idea what might be causing this?

Axios not using react proxy

I'm at my wits end.
I'm creating a react app that connects to a express backend, as per the tutorial I am following I add the proxy to the package.json of the react app:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"axios": "^0.19.2",
"moment": "^2.24.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-moment": "^0.9.7",
"react-redux": "^7.2.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
"uuid": "^7.0.3",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
However when I run a request with axios:
const config = {
headers: {
'Content-Type': 'application/json',
},
};
const body = JSON.stringify({ name, email, password });
const res = await axios.post('/api/users', body, config);
instead of calling localhost:5000/api/users it calls localhost:3000/api/users
I have even tried creating a axios instance:
const axIns = axios.create({
proxy: {
host: '127.0.0.1',
port: 5000,
},
});
Nothing works... please can you tell me what I am doing wrong

Resources