React Hot Toast is not working in class component - reactjs

React-hot-toast v4.1.1 is not working in my class component. Earlier I was using React toastify but now I switched to React hot toast. The react-hot-toast is continously giving the error.
TypeError: Object(...) is not a function
./node_modules/react-hot-toast/dist/react-hot-toast.esm.js
import toast, { Toaster } from "react-hot-toast";
class Toast extends Component {
handleToast = () => {
toast("Toast Created.");
};
render() {
return (
<div>
<Toaster />
<button className="btn btn-primary" onClick={this.handleToast}>
Create Toast
</button>
</div>
);
}
}
export default Toast;
here is the error

this is because there are some conflicting dependencies with react-toastify in the newer versions with respect to its predecessor.
Also if you follow some courses they usually provide some resources to proceed with, when you start working on those resource and do a npm i for its dependencies it install certain versions of the package which is specified in the package.json file, so if you are trying to install a new package as a part of the course it might not be compatible with the ones mentioned in the resource files.
So to avoid conflict here you can manually install all the packages mentioned in package.json with the latest versions then install the latest version of react-toastify
OR
Try reverting the version of react-toastify to earlier version , maybe try with react-toastify#4.1 or the version mentioned in the course. (This worked for me)
I think that if you install an older version of react-toastify, it will work just fine

Related

Unable to use specific React version in a Jupyter Widget

I'm trying to write a Jupyter Widget that uses react based on this cookiecutter. I specifically need to use a certain version of react. However, even when installing a specific version, after building the widget, a different version is used in the jupyterlab frontend.
The react version that is installed in the node_modules folder is the version that I want to use and no other module has react as a dependency. In the node_modules folder, there is also no trace of a different react version to be found.
Specifically, I want to use react 18.2.0. The version that I get by logging React.version in the frontend is react 17.0.2. Obviously, I am not able to use React 18 features like useId which results in an error.
How can I use a different version of react? And where does this other react version 17.0.2 come from?
Reproduce:
Install the cookiecutter as described in the README.
Install react and react-dom via jlpm/yarn add react#18.2.0 react-dom#18.2.0.
Add a simple React component in a new file:
import React from 'react';
export const SimpleComponent = () => {
console.log('React.version:', React.version);
return <div>Simple Test</div>;
};
Render the react component in the render() function of the view in index.ts:
render() {
this.component = React.createElement(SimpleComponent);
ReactDOM.render(this.component, this.el);
}
Console shows: "React.version: 17.0.2"
If you need to change to a specific version of React, you can run this in the terminal:
npm install --save react#18.2.0
Thanks to krassowski for giving me the link to the answer!
In my package.json, I added two entries for react and react-dom to sharedPackages so that I get:
"jupyterlab": {
"extension": "lib/plugin",
"outputDir": "testproject/labextension/",
"sharedPackages": {
"#jupyter-widgets/base": {
"bundled": false,
"singleton": true
},
"react": {
"bundled": true,
"singleton": true
},
"react-dom": {
"bundled": true,
"singleton": true
}
}
}

Not able to import marked js from #types.marked in react app

I have installed marked js in react app using npm install --save #types/marked.
Below is the react component:
import { marked } from "marked";
// import { marked } from "../../node_modules/#types/marked/index";
// import { marked } from "../../node_modules/#types/marked/index.mjs";
// import * as marked from "marked";
const MyPage : React.FC = () => {
return (
<div>
{ marked("Hello world from MyPage.") }
</div>
)
}
export default MyPage;
As you can see in below screenshot of tooltip in VS code, the intellisence knows where the actual function is.
But the app does not compile, I get the following errors (corresponding to different styles of imports for marked in comments):
Module not found: Can't resolve 'marked' in 'D:\coding\myblog\blog-frontend\src\app-components'
Module not found: Can't resolve '../../node_modules/#types/marked/index.mjs' in 'D:\coding\myblog\blog-frontend\src\app-components'
Basically, what ever string that I am providing after from in import statement, react tries to find it in ~/src/app-components. But all other imports are working fine (like my servies, models etc).
I know it's a bit late, but I had the same issue and could resolve it with:
npm install --save marked
In your package.json file you can look up dependencies if it's correct installed.
I used npm install -g marked at first and that wasn't really working despite getting the feedback that it was installed.

A mysterious react hook problem occuring randomly

I have been trying to get react-select to work but somehow i have failed in every turn. What ever i have tried is:
1-create react project using vs2022
2-install react-select using "PM > npm i --save react-select" command
3-modify home.js as in following
import React, { Component } from 'react';
import Select from 'react-select'
export class Home extends Component {
render () {
return (
<div>
<Select />
</div>
);
}
}
4-Hit F5 key.
The above attempt results in Invalid hook call error. I have no idea what i have been doing wrong.
I have realized that aforementioned problem is not something specific to react-select. It exists in every single component that I have tried so far...
Problem is; I was using Package Manager Console ( PM ) which installs components under [PROJECT_DIR]/node_modules directory.
Navigating to [PROJECT_DIR]/clientapp using cd clientapp in Developer Powershell (PS) and then issuing same npm command fixes the problem.

Sharepoint Framework cant find module

I had a project which used youtube-api-search in it. it works there fine.
I have created a sharepoint framework template with yeoman "yo #microsoft/sharepoint" and installed youtube api package as I did in previous project. but when I run this project I encounter an error like below;
Cannot find module 'youtube-api-search'
as I said its working in other react project do i need something specially to make it work here ?
I installed api via "npm i youtube-api-search --save-dev" command
here main component content;
import * as React from 'react';
import { css } from 'office-ui-fabric-react';
import styles from './Announcements.module.scss';
import { IAnnouncementsProps } from './IAnnouncementsProps';
//I have added only these 2 lines to default code
import YTSearch from 'youtube-api-search';
const YOUTUBE_API_KEY = "AIzaSyCI9gcceui5zcQDAEwbyv...";
export default class Announcements extends React.Component<IAnnouncementsProps, void> {
public render(): React.ReactElement<IAnnouncementsProps> {
return (
...
);
}
}
we can import modules in three methods
FIRST::Using Config-->config.json and change
"externals": {
"jquery": "https://code.jquery.com/jquery-3.1.0.min.js",
"OwlCarousel":{
"path":"./node_modules/react-owl-carousel/lib/OwlCarousel.min.js",
"globalName":"OwlCarousel"
},
"Slider":{"path":"./node_modules/react-slick/lib/slider.js",
"globalName":"Sliders"}
},
SECOND:: npm install #types/youtube-api-search --save
THIRD ::
`npm install typings`
`typings install dt~youtube-api-search -global --save`
sometimes dt~ is neccessary sometimes it is not necessaary

how can I create a react component for paypal in-context express checkout?

I am quite new to reacjs and stuggle to create a reactjs component for paypal's in-context express checkout windows - it is simple to do in plain javascript but not sure how I can create a component and use it in similar way as react-stripe-checkout? thanks
Please use react-paypal-express-checkout (I'm the author):
Install:
npm install --save react-paypal-express-checkout
Simplest Example (but it will show the Paypal express check out button):
import React from 'react';
import PaypalExpressBtn from 'react-paypal-express-checkout';
export default class MyApp extends React.Component {
render() {
const client = {
sandbox: 'YOUR-SANDBOX-APP-ID',
production: 'YOUR-PRODUCTION-APP-ID',
}
return (
<PaypalExpressBtn client={client} currency={'USD'} total={1.00} />
);
}
}
==========
There is also a full example, in which you can have all types of callback function (e.g. completed payment-ID, return-URL etc.)
For full detailed document (as well as official guides from Paypal,) please go here:
https://github.com/thinhvo0108/react-paypal-express-checkout

Resources