Im trying to import useHistory from react-router-dom but it keeps giving me the error 'react-router-dom' does not contain an export named 'useHistory'.
React-router-dom's version is 4.3.1 and i've tried updating (in case there was an updated version) but every time i do npm install react-router-dom it always just installs 4.3.1 It updated react-router from 4.3.1 to 5.2.0 just fine so i don't know why react-router-dom doesn't update (if there is an updated version).
If anyone knows another way to import useHistory or make a back button that goes to the previous URL from anywhere I'd love to know.
"useHistory" is replaced by "useNavigate", in "react-router-dom" v.6
import { useNavigate } from 'react-router-dom';
const navigateTo = useNavigate();
navigateTo('/your-page')
You can update react-router-dom package by manually changing the version in package.json to 5.2.0
or
install the specific version by using the below command
npm i react-router-dom#5.2.0
or the latest version like
npm i react-router-dom#latest
Related
With the simplest React app, I can't import axios without seeing this error in my console:
bundle.js:45314 Uncaught SyntaxError: Cannot use import statement outside a module
I created a base app with create-react-app and installed a few packages:
npx create-react-app my-app
# Inside 'my-app/':
yarn install
yarn add axios
(I ran yarn install again just in case.)
I added import axios from "axios" to the default App.js file as shown here:
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import axios from "axios"; // Here is the problem.
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
With axios imported, I get a blank screen and the error message I mentioned above.
This solution wanted me to add a line to index.html, but it didn't do anything for me. Maybe I added it in the wrong place? Here is the line:
<script type="text/javascript" type="module" src="./index.js"></script>
This solution recommended the same line, or adding "type": "module" to package.json, but that also had no affect.
I was using React 18 when I encountered this problem, but I had a working React app using React 17. I switched "react" and "react-dom" to "^17.0.0" in package.json, but it didn't help, either.
Update 2022-11-18:
This is a problem specifically with Axios 1.x.x, and I can "solve" the problem by switching back to a 0.x.x version (0.27.2 is working for me).
Most developers seem to experience it with their Jest tests; see this discussion on GitHub and this other question. The latter was the most informative, but did not solve my problem.
I am not trying to run tests (yet). I'm just trying to use axios in my React app. I can revert to 0.x.x, or I can try using fetch instead. But I would like to understand the source of the problem. I.e. I'd like to understand the implications of this statement:
The 1.x.x version of axios changed the module type from CommonJS to ECMAScript.
(source)
Has anyone been able to make Axios 1.x.x work in a React app?
I am creating a library of components. In this library, I created one component, connected it locally via npm link to my project, all work, the component was displayed. But when I decided to include styled-components to create a component. Here is my component.
import React, {FC} from 'react'
import styled from "styled-components"
import './mytbc.css'
export interface MyButtonProps{
color:string;
big?:boolean;
}
const MyCom: FC<MyButtonProps> = ({children, color, big, ...props}) => {
const MyCommon = styled.button`
background:${color};
padding:10px;
`
return (
<MyCommon>
{children}
</MyCommon>
)
}
export default MyCom
Then errors appeared in the console.
How to fix these errors and what are they related to?
I also had similar case when I was working with lerna and yarn workspaces. In my case the problem was using multiple and different versions of react, some where hoisted some were not during lerna compilation process.
According to docs
In order for Hooks to work, the react import from your application
code needs to resolve to the same module as the react import from
inside the react-dom package.
Run this command to list installed versions of react. And if you see more than one React, you’ll need to figure out why this happens and fix your dependency tree.
npm ls react
OR
yarn list react
Read more about the problem and solutions here
check that you have styled-components in your dependencies in file package.json
by:
cd project_name/src
npm install styled-components
I know people have posted similar question on StackOverflow, but nothing worked for me, the errors are not the same nor are the fixes, so I am creating a new post.
The firm that is testing me sent me their Github repository and I need to check out that code, it is a Telegram Web app and they are using Material-UI for their design, I cloned the repo installed node_modules and then I got the error from Material-UI ,I followed instructions from other StackOverflow posts but nothing worked for me, uninstalled #material-ui/core and #material-ui/icons, installed them again and still got the same error.
I have tried both NPM and Yarn for installation and nothing. Hope you can help, it is important.
./src/Theme.js
Module not found: Can't resolve '#material-ui/core/styles/createMuiTheme' in '/Users/Faruk/Desktop/int/telegram-react/src'
As #Ryan mentioned, createMuiTheme has been renamed to createTheme in the latest v4 and in v5. See the migration guide here for reference.
V5
import { createTheme } from '#mui/material/styles';
V4
import { createTheme } from '#material-ui/core/styles';
If you are using material UI V5 or higher, this works;
import { createTheme } from '#mui/material/styles';
Don't know about createMuiTheme. but you can use this for importing Styles in Mui. This solved mine on importing styles only,
import styled from '#material-ui/core';
Also it works for Material-ui Box, Paper, Link, Grid all of them. Use like this,
import { Grid, Link, makeStyles, Paper,Box, styled} from '#material-ui/core';
But I'm not sure with your theme .
When trying to import useNavigate from react-router-dom, I get the following error:
Attempted import error: 'useNavigate' is not exported from 'react-router-dom'.
My Import statement:
import { useNavigate } from 'react-router-dom';
You are trying to use the latest features of react-router.
Please make sure that you installed the react-router-dom#6.0.0-alpha.2 .
It is React Router v6 which gives you a useNavigate hook
Please refer here for further reading from the React Router team
Two quick ways to check the version:
Verify from the package.json file
Run npm list --depth=0 to view the various packages in your project
If you're using react router v5 you can use useHistory.
import { useHistory } from "react-router-dom";
use:
history.push('/your-component')
I faced issue as like this: "useNavigate is not exported from 'react-router-dom"
My solution - I uninstalled the react-router-dom and I have reinstalled it... now this issue is resolved for me..
I am trying to convert this simple application to support react router https://github.com/jackfranklin/universal-react-example to v4.1.1.
In the package.json all I changed is the following:
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router": "^4.1.1"
Everything stopped working. Could you please help me build this app working in React Router v4.1.1? There is no proper documentation for react routerv4.1.1 server side rendering. So struck with this new version.
Since React Router v4.1.1 is a major change, answering this question will help a lot of us, in the form of first proper tutorial for
React Router v4.1.1.
React Router v4 is very different from the previous versions, you can't just change the version number and expect everything to work smoothly. Go through the docs and the examples. Change the app step by step.
Useful links:
https://reacttraining.com/react-router/web/guides/server-rendering
http://rants.broonix.ca/upgrading-to-react-router-v4/
Moving from react router 3.x to 4.x
A few things changed when React Router 4!
First, the names of the packages changed. So you have to update your imports from 'react-dom' to 'react-router-dom'. You still need to include 'react' and 'react-router' as well.
Second, there are multiple Browsers now. So you have to import BrowserRouter instead of just Router. (You can also use HashRouter, but I'd say start with BrowserRouter.)
See:
import {
BrowserRouter as Router,
Route
} from 'react-router-dom';
Start there and keep going. Good luck!