クリックしたボタンに応じてコンポーネントを表示しようとしていますが、この行に問題があります。Reactjs - エラーを返すメソッド
{this.showTab({this.state.active})}
構文エラーについては、エラーが発生します。何が間違っていて、何かをクリックすることによって、コンポーネントの1つを表示する良い方法があります<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
文字列を返すためです。場合によっては 'return 'を実行してください。 –
Li357