我是 ReactJS 的新手,在渲染一个用于在单独组件中显示/隐藏模式的 ModalTrigger 时遇到问题。我使用的样板是 react-starter-kit。
返回的错误是:“不变违规:元素类型无效:期望字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。检查渲染方法MenuButton
。”
在返回之前似乎不太可能将组件转换为字符串......这是代码:
import React, {PropTypes, Component} from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import ModalTrigger from 'react-bootstrap';
import Menu from '../Menu';
import s from './MenuButton.css';
class MenuButton extends Component {
static getInitialState() {return {isMenuOpen: false}};
constructor() {
super();
this.state = {isMenuOpen: false};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
if(this.state) {
this.setState({isMenuOpen: !(this.state.isMenuOpen)});
}
}
render() {
var btnImage = require('./menuButton.svg');
var show = {show: this.state};
console.log('menu ' + <Menu/>);
return (
<ModalTrigger modal={<Menu />}>
<div className={s.menuBtn}>
<a onClick={this.handleClick.bind(this)}>
<img className={s.menuBtnImg} src={btnImage}/> {show} {this.state.isMenuOpen ? 'Stäng meny' : 'Meny'}
</a>
</div>
</ModalTrigger>
);
}
}
export default withStyles(s)(MenuButton);
然后是模态:
import React, { Component } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Menu.css';
class Menu extends Component {
static getInitialState() {return {show: false}};
static showModal(){
this.setState({show: true});
};
static hideModal() {
this.setState({show: false});
}
render() {
if (!this.state.show) {
return <span/>;
}
return (
<Modal {...this.props} title='Meny' animation={true}>
<div className='modal-body'>
<ul>
<a href="#main"><li>Startsida</li></a>
<a href="#omoss"><li>Om oss </li></a>
<a href="#reminiscens"><li>Reminiscens</li></a>
<a href="#appen"><li>Appen</li></a>
<a href="#stories"><li>Berättelser</li></a>
<a href="#nyheter"><li>Nyheter om projektet</li></a>
<a href="#englishinfo"><li>Info in English</li></a>
</ul>
</div>
</Modal>
);
}
}
export default withStyles(s)(Menu);