TypeScript error while using react-switch.
See original GitHub issueHi,
I am having the following error when I try to use this library.
The following code:
import * as React from "react";
import Component from "../../AbstractComponent";
import Switch from "react-switch";
export class SwitchComponent extends Component {
public state = {
checked: false
};
constructor(public props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
public handleChange(checked) {
this.setState({
checked: checked
}, () => {
if (this.props.onChange) {
this.props.onChange.call(this, this.state);
}
});
}
public render() {
return (
<label htmlFor="normal-switch" className="switch-container">
<Switch
checked={this.state.checked}
onChange={this.handleChange}
onColor="#86d3ff"
offColor="#B2BEC4"
onHandleColor="#2693e6"
handleDiameter={20}
uncheckedIcon={false}
checkedIcon={false}
boxShadow="0px 1px 5px rgba(0, 0, 0, 0.6)"
activeBoxShadow="0px 0px 1px 10px rgba(0, 0, 0, 0.2)"
height={20}
width={40}
className="react-switch"
/>
</label>
);
}
}
produces:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of
Component.
It works when I use * import syntax, but TypeScript gives the following error in that case.
`JSX element type ‘Switch’ does not have any construct or call signatures.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
React typescript onChange is showing error when I try to add ...
When I turn on the switch it need to show the component and when I turn odd the switch it need to hide...
Read more >Avoiding Switch Statements in React/TypeScript - Medium
Switch statements replace long if-else chains. They make it easy to return different results depending on the value of some variable.
Read more >Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates...
Read more >Safer Exhaustive Switch Statements in TypeScript - Meticulous
A guide on various methods and techniques for implementing safer switch and if/else statements, including use of the never type and eslint ...
Read more >TypeScript errors and how to fix them
TS1471: Module ' @headlessui/react ' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just ran into this issue and it was very confusing because of how obscure the error message is. I would strongly recommend switching to the “export namespace” form that React uses. Semantic versioning is suppose to allow you to make breaking changes, so I would recommend doing this for v5.0. At least add documentation to the README to add
esModuleInterop: true
to the tsconfig.json.I took your advice and made the change for 5.0.0 which was just released. This should no longer be a problem.