Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应物料界面模式未打开_Javascript_Reactjs_Material Ui_React Material - Fatal编程技术网

Javascript 反应物料界面模式未打开

Javascript 反应物料界面模式未打开,javascript,reactjs,material-ui,react-material,Javascript,Reactjs,Material Ui,React Material,我试图从MaterialUI中组合一个react组件,它打开一个模式。我有切换的状态,我在chrome中的react developer控制台中看到切换从false变为true,但当我单击按钮时,模式不会打开(按钮仅更改react控制台验证的状态) 从“react”导入react 从“@material ui/core/Modal”导入模态; 从“@material ui/core/Button”导入按钮; 从“@material ui/core/styles”导入{withStyles}”;

我试图从MaterialUI中组合一个react组件,它打开一个模式。我有切换的状态,我在chrome中的react developer控制台中看到切换从false变为true,但当我单击按钮时,模式不会打开(按钮仅更改react控制台验证的状态)

从“react”导入react
从“@material ui/core/Modal”导入模态;
从“@material ui/core/Button”导入按钮;
从“@material ui/core/styles”导入{withStyles}”;
函数rand(){
返回Math.round(Math.random()*20)-10;
}
函数getModalStyle(){
常数top=50+rand();
常数左=50+兰德();
返回{
top:`${top}%`,
左:`${left}%`,
transform:`translate(${top}%,-${left}%)`,
};
}
常量样式=((主题)=>({
论文:{
位置:'绝对',
宽度:400,
背景色:theme.palete.background.paper,
边框:“2px实心#000”,
boxShadow:theme.shadows[5],
填充:主题。间距(2,4,3),
},
}));
类ModalTest扩展了react.Component{
状态={
modalToggle:错误
}
componentDidMount(){
}
handleOpen=()=>this.setState({modalToggle:true})
handleClose=()=>this.setState({modalToggle:false})
renderModalBody(){
返回(
情态语篇

这是对模态的测试。

); } renderActionButtons(){ 返回打开模式 } renderModal(){ {this.renderModelbody()} } render(){ 返回此。renderActionButtons() } } 使用样式导出默认值(样式,{withTheme:true})(ModalTest)
正如@Christian Fritz所说,您没有调用该方法来渲染模型

现在,您可以像这样在渲染返回中进行渲染

render(){
return (
   <Button onClick={this.handleOpen} style={{margin: 0, position: 'absolute',
                                                top: '50%', left: '50%' }} 
   variant="contained" color="primary">Open Modal</Button>
    <Modal open={this.state.modalToggle} onClose={this.handleClose}>{this.renderModalBody()}</Modal>
    )

}
render(){
返回(
开放模态
{this.renderModelbody()}
)
}
因为material ui在它的值为真之前不会打开模型,我建议您使用功能组件,因为它更简单,而且可以更好地处理,因为每个状态都是由它自己管理的,您可以做几乎与类组件相同的事情,加上它似乎反映了团队尝试更多地推动功能组件的使用 我写的示例代码是函数组件:

export default UserShow;
const ModelTest = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button
        onClick={() => setOpen(true)}
        style={{ margin: 0, position: "absolute", top: "50%", left: "50%" }}
        variant="contained"
        color="primary"
      >
        Open Modal
      </Button>

      <Modal open={open} onClose={() => setOpen(false)}>
        <div style={getModalStyle()} className={styles.paper}>
          <h2 id="simple-modal-title">Text in a modal</h2>
          <p id="simple-modal-description">This is a test of modal.</p>
        </div>
      </Modal>
    </>
  );
};
导出默认UserShow;
常量ModelTest=()=>{
const[open,setOpen]=useState(false);
返回(
setOpen(真)}
样式={{边距:0,位置:“绝对”,顶部:“50%”,左侧:“50%”}
variant=“包含”
color=“primary”
>
开放模态
setOpen(false)}>
情态语篇

这是模态测试

); };
正如@Christian Fritz所说,您没有调用该方法来渲染模型

现在,您可以像这样在渲染返回中进行渲染

render(){
return (
   <Button onClick={this.handleOpen} style={{margin: 0, position: 'absolute',
                                                top: '50%', left: '50%' }} 
   variant="contained" color="primary">Open Modal</Button>
    <Modal open={this.state.modalToggle} onClose={this.handleClose}>{this.renderModalBody()}</Modal>
    )

}
render(){
返回(
开放模态
{this.renderModelbody()}
)
}
因为material ui在它的值为真之前不会打开模型,我建议您使用功能组件,因为它更简单,而且可以更好地处理,因为每个状态都是由它自己管理的,您可以做几乎与类组件相同的事情,加上它似乎反映了团队尝试更多地推动功能组件的使用 我写的示例代码是函数组件:

export default UserShow;
const ModelTest = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button
        onClick={() => setOpen(true)}
        style={{ margin: 0, position: "absolute", top: "50%", left: "50%" }}
        variant="contained"
        color="primary"
      >
        Open Modal
      </Button>

      <Modal open={open} onClose={() => setOpen(false)}>
        <div style={getModalStyle()} className={styles.paper}>
          <h2 id="simple-modal-title">Text in a modal</h2>
          <p id="simple-modal-description">This is a test of modal.</p>
        </div>
      </Modal>
    </>
  );
};
导出默认UserShow;
常量ModelTest=()=>{
const[open,setOpen]=useState(false);
返回(
setOpen(真)}
样式={{边距:0,位置:“绝对”,顶部:“50%”,左侧:“50%”}
variant=“包含”
color=“primary”
>
开放模态
setOpen(false)}>
情态语篇

这是模态测试

); };
关于陈述显而易见的危险:您没有在渲染函数中调用renderModal。因此,模态根本不存在。有一点很明显:您没有在渲染函数中调用renderModal。所以模态根本不存在。