反应调用另一个组件
React calling another component
我正在使用 Grommet 并试图让 Layer(几乎是模态)在按下按钮时工作。我知道我的 onClick 有效,因为我尝试了一个简单的 console.log 并且它有效。如果我使用 ReactDOM 并渲染它,也可以显示 MyModal。我认为我的问题与我如何调用它或返回它有关?我希望在单击按钮时显示模式。
MyModal.js
import React, { Component } from 'react';
import Layer from 'grommet/components/Layer';
import Header from 'grommet/components/Header';
import Heading from 'grommet/components/Heading';
import Section from 'grommet/components/Section';
import Paragraph from 'grommet/components/Paragraph';
export default class MyModal extends Component {
render () {
return (
<Layer closer={true} align="top">
<Header>
<Heading tag="h2">
Title
</Heading>
</Header>
<Section>
<Paragraph>
This is a simple dialog.
</Paragraph>
</Section>
</Layer>
);
}
};
Main.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import App from 'grommet/components/App';
import Button from 'grommet/components/Button';
import MyModal from './components/MyModal';
export default class Main extends Component {
getComponent(event) {
return <MyModal/>;
}
render () {
return (
<App centered={false}>
<Button onClick={this.getComponent.bind(this)} label="Action" />
</App>
);
}
};
问题:
您正在尝试将模态 呈现为 内联 onClick
处理程序。
建议的解决方案:
设置状态值以在显示模式时处理
设置 onClick
以切换此值
使用此状态调用 render 中的另一个方法以有条件地渲染 Modal
您的代码可以修改为:
export default class Main extends Component {
constructor(props) {
super(props);
this.state = {
showModal: false // set a value in state to store whether or
// not to show the Modal
};
// I've just put these binding in the constructor
// so as not to clock up the render method and they only
// get called once during the lifetime of the component
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) { // switch the value of the showModal state
this.setState({
showModal: !this.state.showModal
});
}
getComponent() {
if (this.state.showModal) { // show the modal if state showModal is true
return <MyModal/>;
} else {
return null;
}
}
render() {
return (
<App centered={false}>
<Button onClick={this.handleClick} label="Action"/><br/> <b>{this.getComponent}</b> // call the method to render the modal here.
</App>
);
}
};
/
我正在使用 Grommet 并试图让 Layer(几乎是模态)在按下按钮时工作。我知道我的 onClick 有效,因为我尝试了一个简单的 console.log 并且它有效。如果我使用 ReactDOM 并渲染它,也可以显示 MyModal。我认为我的问题与我如何调用它或返回它有关?我希望在单击按钮时显示模式。
MyModal.js
import React, { Component } from 'react';
import Layer from 'grommet/components/Layer';
import Header from 'grommet/components/Header';
import Heading from 'grommet/components/Heading';
import Section from 'grommet/components/Section';
import Paragraph from 'grommet/components/Paragraph';
export default class MyModal extends Component {
render () {
return (
<Layer closer={true} align="top">
<Header>
<Heading tag="h2">
Title
</Heading>
</Header>
<Section>
<Paragraph>
This is a simple dialog.
</Paragraph>
</Section>
</Layer>
);
}
};
Main.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import App from 'grommet/components/App';
import Button from 'grommet/components/Button';
import MyModal from './components/MyModal';
export default class Main extends Component {
getComponent(event) {
return <MyModal/>;
}
render () {
return (
<App centered={false}>
<Button onClick={this.getComponent.bind(this)} label="Action" />
</App>
);
}
};
问题:
您正在尝试将模态 呈现为 内联 onClick
处理程序。
建议的解决方案:
设置状态值以在显示模式时处理
设置
onClick
以切换此值使用此状态调用 render 中的另一个方法以有条件地渲染 Modal
您的代码可以修改为:
export default class Main extends Component {
constructor(props) {
super(props);
this.state = {
showModal: false // set a value in state to store whether or
// not to show the Modal
};
// I've just put these binding in the constructor
// so as not to clock up the render method and they only
// get called once during the lifetime of the component
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) { // switch the value of the showModal state
this.setState({
showModal: !this.state.showModal
});
}
getComponent() {
if (this.state.showModal) { // show the modal if state showModal is true
return <MyModal/>;
} else {
return null;
}
}
render() {
return (
<App centered={false}>
<Button onClick={this.handleClick} label="Action"/><br/> <b>{this.getComponent}</b> // call the method to render the modal here.
</App>
);
}
};
/