2
我试图显示一个组件,具体取决于哪个按钮被点击,但有这条线的问题。Reactjs - 给出错误的方法
{this.showTab({this.state.active})}
它会抛出一个关于语法错误的错误。我做错了什么,有没有更好的方式来显示组件<Grids />
,<Pics />
或<Maths />
取决于我点击。
import React, {Component} from 'react';
import Pics from './pics';
import Grids from './grids';
import Maths from './maths';
import { ButtonToolbar } from 'react-bootstrap';
import { Button } from 'react-bootstrap';
export default class Tabs extends Component {
constructor(props) {
super();
this.state = {
count: 0,
active: null
};
this.handleClick = this.handleClick.bind(this);
this.showTab = this.showTab.bind(this);
}
handleClick(value) {
this.setState({
count: this.state.count + 1,
active: value
});
}
showTab(tab) {
switch(tab) {
case "Grids":
return "<Grids />";
break;
case "Pics":
return "<Pics />";
break;
default:
return "<Maths />";
}
}
render() {
return (
<div>
<ButtonToolbar>
{this.props.tabs.map(listValue =>
<Button onClick={this.handleClick.bind(this, listValue)}>
{listValue}
</Button>
)}
</ButtonToolbar>
{this.showTab({this.state.active})}
</div>
);
}
}
摆脱围绕'this.state.active'的括号。 – Li357
感谢 - 它的工作 - 但现在它只显示“ ”,而不是呈现它。我将如何解决这个问题?我会更新上面的问题。 –
Wasteland
因为你返回一个字符串。只要在案件中返回 '。 –
Li357