尝试学习React和Reactstrap并尝试让Modals工作。当我单击按钮时,它应该切换Modal,但是现在它给了我这个错误:元素类型无效:期望一个字符串(用于内置组件)或一个类/函数(用于复合组件)但得到了:未定义。检查' App'
的渲染方法无法弄清楚我做错了什么。有人可以帮我告诉我这里出错的地方吗?感谢您提供的任何帮助。
如果可能,我想使用最新版本的React和Reactstrap。
这是Codepen上的链接:https://codepen.io/lieberscott/pen/ddYNVP
const { Button,
Container,
Modal,
ModalTitle,
ModalHeader,
ModalBody,
ModalFooter } = Reactstrap;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
showModal: false
}
this.toggleModal = this.toggleModal.bind(this);
}
toggleModal() {
console.log("hello");
this.setState({
showModal: !this.state.showModal
})
}
render() {
return (
<Container>
<Headline />
<Box />
<Button outline color="primary" onClick={this.toggleModal}>Click</Button>
<Modal isOpen={this.state.showModal} toggle={this.toggleModal} className="modal">
<ModalHeader>
<ModalTitle id="modalTitle">
Add a Recipe
</ModalTitle>
</ModalHeader>
<ModalBody>
Modal body
</ModalBody>
<ModalFooter>
Modal footer
</ModalFooter>
</Modal>
</Container>
);
}
}
class Headline extends React.Component {
render() {
return (
<div>
Recipes
</div>
);
}
}
class Box extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [
{
name: "Tofu tacos",
ingredients: [
"Shells",
"lettuce",
"tofu",
"paprika"
]
},
{
name: "Spaghetti",
ingredients: [
"pasta",
"sauce",
"salt"
]
}
] // end of items
} // end of this.state
} // end of constructor
render() {
const allitems = this.state.items.map(item => {
return (
<div>
{item.name}
</div>
);
})
return (
<div>
{allitems}
</div>
);
}
}
const app = document.getElementById("app");
ReactDOM.render(<App />, app);
答案 0 :(得分:0)
对不起,我不知道如何通过Codepen共享我的代码。
我做了这些更改:
toggleModal() {
console.log("hello");
console.log( 'before setState: ', this.state );
this.setState({
showModal: !this.state.showModal
})
console.log( 'after setState: ', this.state );
}
Button onClick事件,当您onClik={this.something}
执行此操作this
时,它引用的是Button元素。我改为:
<Button outline color="primary" onClick={() => this.toggleModal()}>Click</Button>
当我onClick={ () => this.something() }
允许我使用班级的方法时。
只需查看console.log输出,您就会看到单击按钮将showModal更改为true
或false
。
看看头颅化的答案:https://forum.freecodecamp.org/t/difference-in-reactjs-onclick-function-binding/116027/2
答案 1 :(得分:-1)
Reactstrap Modal不显示,因为需要将淡入淡出设置为False。请看我的代码谁工作。
class Demoextends extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: false,
fade: false
};
this.toggle = this.toggle.bind(this);
};
toggle() {
console.log("hello");
this.setState({
modal: !this.state.modal
});
console.log( 'after setState: ', this.state );
}
render() {
return (
<div>
<Button color="danger" onClick={this.toggle}>Launch</Button>
<Modal isOpen={this.state.modal} fade={this.state.fade } toggle={this.toggle}>
<ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
<ModalBody>
</ModalBody>
<ModalFooter>
<Button onClick={this.toggle}>Do Something</Button>{' '}
<Button onClick={this.toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
导出默认的演示扩展;
当然,您必须在代码中添加及部分reactstrap
答案 2 :(得分:-1)
这种方式要容易得多
在构造函数示例中添加您的状态
this.state={showLogOut: false}
您只需返回setState true示例
<Button onClick={() => this.setState({showLogOut: true})}>Show modal</Button>
现在我们只需要模态代码,因为我们有一个状态和一个setState
<Modal isOpen={this.state.showLogOut} fade="false" toggle={() => this.setState({showLogOut: false})}>
<ModalHeader toggle={() => this.setState({showLogOut: false})}>Ready to leave?</ModalHeader>
<ModalBody>
<p>Press logout to end session.</p>
</ModalBody>
<ModalFooter>
<Button onClick={() => this.setState({showLogOut: false})}>Cancel</Button>
<Button color="info">Logout</Button>
</ModalFooter>
这应该足以显示您的模态:我假设您正在使用react-strap,并且您正在导入所需的元素,但只是要确保此处是您需要导入的内容
import { Button, Modal, ModalBody, ModalFooter, ModalHeader} from 'reactstrap';