【发布时间】:2017-02-01 23:11:52
【问题描述】:
UPDATE 解决得不好
const curSlide = this.props.name;
const setView = () => {
if(curSlide === 'Slide1') {
return <Slide1 />
} else if(curSlide === 'Slide2') {
return <Slide2 />
} else {
return <Slide3 />
}
}
return(
<div>
<center>
{setView()}
</center>
</div>
我有一个故事,其中包含一个承载不同组件的幻灯片容器。我的故事容器下有一个按钮,按下时会将 this.props.name + 1 传递给幻灯片容器。我希望幻灯片容器能够使用此信息并根据按钮单击呈现出正确的幻灯片。
我不确定为什么单击按钮时没有显示任何内容。
Story.js
import React, { Component, PropTypes } from 'react'
import sass from '../scss/application.scss'
import SlideContainer from './SlideContainer'
class Story extends Component {
constructor(props){
super(props)
this.state = {
name: '',
count: 0
}
}
handleClick(e) {
e.preventDefault();
let tempCount = this.state.count + 1;
let curSlide = `Slide${tempCount}`;
this.setState({
name: curSlide,
count: tempCount
});
}
render() {
return(
<div>
<div>
<SlideContainer name={this.state.name}/>
</div>
<center><button className="btn btn-danger" onClick={this.handleClick.bind(this)}>
<span className="glyphicon glyphicon-heart"></span></button>
</center>
</div>
)
}
}
Story.propTypes = {
name: PropTypes.string,
count: PropTypes.number
}
export default Story;
SlideContainer.js
import React, { Component, PropTypes } from 'react'
import sass from '../scss/application.scss'
import Slide1 from './Slide1'
import Slide2 from './Slide2'
class SlideContainer extends Component {
constructor(props){
super(props)
this.state = {
name: ''
}
}
render() {
let curSlide = this.props.name;
return(
<div>
<center>
<curSlide />
</center>
</div>
)
}
}
SlideContainer.propTypes = {
name: PropTypes.string
}
export default SlideContainer;
【问题讨论】:
-
几个 OT cmets -
我认为自 90 年代以来已被弃用。 Story.propTypes 也没有意义......故事有状态。 -
center 仍然可以正常工作,我应该将文本添加到 div 还是现在如何将 div 居中?
-
通常所有样式都应该通过 CSS 完成 - 理论上浏览器可能会在某个时候开始放弃对
的支持。
标签: reactjs button components