Material-UI@next中Toggle组件导入方法及v1.0.0-beta.26模块找不到问题咨询
Hey there, let's sort out this import problem you're hitting with Material-UI v1.0.0-beta.26 (part of the @next release line)!
The core reason you're seeing a "module not found" error is two-fold: the old Toggle component has been renamed, and the package structure for Material-UI v1 is completely different from earlier versions. Here's how to fix it:
1. Use the Correct Component & Import Path
In Material-UI@next (v1 and later), the component you're looking for is now called Switch, and all core components live under the @material-ui/core package. Replace your old import line with this:
import Switch from '@material-ui/core/Switch';
2. Example Usage
Once imported, you can use the Switch component just like you would have used the old Toggle. Here's a quick functional component example to get you started:
import React from 'react'; import Switch from '@material-ui/core/Switch'; function MyToggleComponent() { const [isChecked, setIsChecked] = React.useState(false); const handleToggle = (event) => { setIsChecked(event.target.checked); }; return ( <Switch checked={isChecked} onChange={handleToggle} inputProps={{ 'aria-label': 'example toggle' }} /> ); } export default MyToggleComponent;
Quick Heads-Up on v1 Breaking Changes
Material-UI v1 introduced a ton of breaking updates from pre-v1 versions. A few other common component renames to keep in mind:
FlatButton→Buttonwithvariant="text"orvariant="contained"RaisedButton→Buttonwithvariant="contained"IconButtonstays the same, but imports from@material-ui/core/IconButton
If you run into other import issues, double-check the component's name and path against the Material-UI v1 component documentation.
内容的提问来源于stack exchange,提问作者Hemadri Dasari

