I am trying to use https://github.com/facebook/prop-types
So I have also installed the @types/prop-types for it. https://www.npmjs.com/package/@types/prop-types
But I guess this error. [ts] Module '"/node_modules/@types/prop-types/index"' has no default export.
What I am trying to accomplish is what is being done in the withRouter documentation. https://reacttraining.com/react-router/web/api/withRouter
For example you see in their JavaScript the use of PropTypes:
import React from 'react'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'
// A simple component that shows the pathname of the current location
class ShowTheLocation extends React.Component {
static propTypes = {
match: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
}
render() {
const { match, location, history } = this.props
return (
<div>You are now at {location.pathname}</div>
)
}
}
// Create a new component that is "connected" (to borrow redux
// terminology) to the router.
const ShowTheLocationWithRouter = withRouter(ShowTheLocation)
Any help on this is appreciated!
If a value is exported as a default export, it has to be imported as a default import and if it's exported as a named export, it has to be imported as a named import. Use the search field on my Home Page to filter through my more than 1,000 articles.
The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule'. Here is an example of how the error occurs. This is our index.ts file. Copied!
You have to use import * as React from "react"; instead of import React from 'react'. That happens because babel (the one that you were using before) assumes modules.export as default export while typescript (the one that you are using now) does not.
You can have a maximum of 1 default export per file. If you are exporting a variable as a default export, you have to declare it on 1 line and export it on the next. You can't declare and default export a variable on the same line. Copied! You can only have a single default export per module, but you can have multiple named exports.
You need to modify your import statement like so
import * as PropTypes from 'prop-types'
What this says is create an object PropTypes
, and import all the exports in the prop-types
module into the PropTypes
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With