【发布时间】:2017-11-28 06:18:54
【问题描述】:
我有一个课程页面,其中包含学生列表作为按钮。单击时,按钮应呈现 ShowStudentInfo,但我的 ShowStudentInfo 组件在嵌套在内部时不会在单击时呈现(如果 state.isClicked)。在条件之外它工作正常,但我需要有条件,否则一切都会显示在课程页面渲染中。
主要课程组件
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions/index';
import { Redirect } from 'react-router';
import { Link } from 'react-router-dom';
import ShowStudentInfo from '../ShowStudentInfo/ShowStudentInfo'
class CoursePage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
course: {},
student: {},
isClicked: false
};
console.log(this.props.match.params.cuid)
this.onClick = this.onClick.bind(this)
}
onClick(event) {
event.preventDefault()
console.log(this.state.isClicked)
this.setState({
isClicked: !this.state.isClicked
})
}
componentDidMount() {
this.props.dispatch(actions.getCourse(this.props.match.params.cuid));
this.props.dispatch(actions.getStudents());
}
render() {
let studentList = this.props.student
const students = Object.keys(studentList).map(student => studentList[student])
const currentStudents = students.map(student => {
if ((this.props.match.params.cuid) == student.courses) {
return (
<p>
<button className="students" id={student._id} onClick={this.onClick}>{student.firstName} {student.lastName}</button>
</p>
)
if (this.state.isClicked) {
return (
<div className="student-info">
<ShowStudentInfo firstName={student.firstName}
lastName={student.lastName} phoneNumber={student.phoneNumber} />
</div>
)
}
}
})
return (
<div>
<h1>{this.props.course.name}</h1>
<Link to={`/addStudent/${this.props.match.params.cuid}`}> Add a new student</Link>
<div className="studentList">Your students{currentStudents} </div>
</div>
);
}
}
const mapStateToProps = (state, props) => {
return {
course: state.course.course,
student: state.student.students
}
}
export default connect(mapStateToProps)(CoursePage)
我的 ShowStudentInfo 组件
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions/index';
import { Redirect } from 'react-router';
import { Link } from 'react-router-dom';
class ShowStudentInfo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
student: {
firstName: '',
lastName: '',
phoneNumber: ''
},
isClickedEdit: false,
isClickedDelete: false
}
this.isClickedEdit = this.isClickedEdit.bind(this)
this.isClickedDelete = this.isClickedDelete.bind(this)
}
isClickedEdit(event) {
this.setState({
isClickedEdit: true
})
}
isClickedDelete(event) {
this.setState({
isClickedDelete: true
})
}
render() {
return (
<div className="student-info-container">
<p>Name: {this.props.firstName} {this.props.lastName}</p>
<p>Phone Number: {this.props.phoneNumber}</p>
</div>
)
}
}
if (this.state.isClicked) {
return (
<div className="student-info">
<ShowStudentInfo firstName={student.firstName}
lastName={student.lastName} phoneNumber={student.phoneNumber} />
</div>
)
}
是最重要的部分,信息在没有 onClick 条件的情况下正常呈现。
【问题讨论】:
标签: reactjs redux react-router react-redux