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