我希望在this.state.currentlySelected >= 1
可以在下面找到操作员
import React from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import update from 'immutability-helper'
import { nextSlide } from '../../../../actions'
import Slide from '../../../../components/Slide'
import Topic from '../../../../components/Topic'
import css from './index.scss'
// Universal slide styles
import { leftPanel, rightPanel, panelGuide } from '../../index.scss'
class TopicSlide extends React.Component {
constructor(props) {
super(props)
this.state = {
topics: {
data: [
{ id: 0, name: 'Expressions, equations and functions' },
{ id: 1, name: 'Real numbers' },
{ id: 2, name: 'Solving linear equations' },
{ id: 3, name: 'Linear functions' },
{ id: 4, name: 'Factoring and polynomials' },
{ id: 5, name: 'Linear equations' },
{ id: 6, name: 'Linear inequalitites' },
{ id: 7, name: 'Systems of linear equations and inequalities' },
{ id: 8, name: 'Exponents and exponential functions' },
{ id: 9, name: 'Quadratic equations' },
{ id: 10, name: 'Radical expressions' },
{ id: 11, name: 'Rational expressions' },
],
maxSelectable: 1,
currentlySelected: 0, // count of how many are selected
},
}
}
selectTopic = (id) => {
if (this.state.topics.currentlySelected < this.state.topics.maxSelectable) {
this.setState((state) => {
const newTopics = update(state.topics, {
data: { [id]: { selected: { $set: true } } },
})
newTopics.currentlySelected = state.topics.currentlySelected + 1
return { topics: newTopics }
})
}
}
unselectTopic = (id) => {
this.setState((state) => {
const newTopics = update(state.topics, {
data: { [id]: { selected: { $set: false } } },
})
newTopics.currentlySelected = state.topics.currentlySelected - 1
return { topics: newTopics }
})
}
render() {
return (
<Slide className={css.topicslide}>
<div className={leftPanel}>
<div className={panelGuide}>
<h2 className={css.topicslide__info__header}>
Begin by selecting <i>Factoring and Polynomials</i> as your topic.
</h2>
{
this.state.currentlySelected >= 1
&& (
<button
className={css.topicslide__info__continue}
onClick={() => this.props.dispatch(nextSlide())}
>
Continue
</button>
)
}
</div>
</div>
<div className={rightPanel}>
<div className={css.topicslide__topics}>
<h3 className={css.topicslide__topics__header}>Algebra 1 Topics</h3>
<ul className={css.topicslide__topics__list}>
{this.state.topics.data.map(topic => (
<li key={topic.id}>
<Topic
id={topic.id}
selected={!!topic.selected}
onSelect={this.selectTopic}
onUnselect={this.unselectTopic}
>
{topic.name}
</Topic>
</li>
))}
</ul>
</div>
</div>
</Slide>
)
}
}
TopicSlide.propTypes = {
dispatch: PropTypes.func.isRequired,
}
export default connect()(TopicSlide)
当this.state.currentlySelected
增加到1
但是没有错误,我怀疑它与我使用this.state
我认为这是正确的吗?
答案 0 :(得分:0)
根据您的代码,currentlySelected
属性属于state.topics
,而不属于状态根本身。所以
{
this.state.currentlySelected >= 1
&& (
<button
className={css.topicslide__info__continue}
onClick={() => this.props.dispatch(nextSlide())}
>
Continue
</button>
)
}
应该是
{
this.state.topics.currentlySelected >= 1
&& (
<button
className={css.topicslide__info__continue}
onClick={() => this.props.dispatch(nextSlide())}
>
Continue
</button>
)
}
答案 1 :(得分:0)
如果它像这样
会更好{this.state.topics.currentlySelected >= 1 ? (
<button
className={css.topicslide__info__continue}
onClick={() => this.props.dispatch(nextSlide())}
>
Continue
</button>
) : null}