当前位置: 首页 > 知识库问答 >
问题:

我的组件应该显示/隐藏时,一个按钮被点击没有渲染点击

丌官子安
2023-03-14

我有一个课程页面,上面有学生列表作为按钮。单击时,按钮应该呈现ShowStuentInfo,但是当嵌套在内部时(如果state.is单击),我的ShowStuentInfo组件不会在单击时呈现。在条件之外,它工作得很好,但我需要条件,否则一切都会显示在课程页面渲染上。

主菜成分

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条件的情况下正常呈现。

共有2个答案

秦胡媚
2023-03-14

这只是你逻辑中的一点重构:)希望它能帮助你。

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 {

  state = {
    course: {},
    student: {},
    isClicked: false
  };

  onClick = (event) => {
    event.preventDefault()
    console.log(this.state.isClicked)
    this.setState({
      isClicked: !this.state.isClicked
    })
  }

  getCurrentStudents = (students) => {
    const { match } = this.props
    const { isClicked } = this.state

    return students.map(student => {
      if (match.params.cuid == student.courses) {
        return (
          <div>
            <p><button className="students" id={student._id} onClick={this.onClick}>
              {student.firstName} {student.lastName}
            </button></p>
            {isClicked && this.getStudentInfo(student) }
          </div>
        )
      }
    })
  }

  getStudentInfo = (student) => (
    <div className="student-info" >
      <ShowStudentInfo
        firstName={student.firstName}
        lastName={student.lastName}
        phoneNumber={student.phoneNumber} />
    </div>
  )


  componentDidMount() {
    let { match, dispatch } = this.props
    dispatch(actions.getCourse(match.params.cuid));
    dispatch(actions.getStudents());
  }

  render() {
    let { studentList, match, course } = this.props
    const students = Object.keys(studentList).map(student => studentList[student])
    const currentStudents = this.getCurrentStudents(students)

    return (
      <div>
        <h1>{course.name}</h1>
        <Link to={`/addStudent/${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)
邢浩邈
2023-03-14

map函数包含2个返回函数,而第一个返回将导致它转到当前迭代过程中的下一项

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>
    )
    // this will never hit in case the previous statement is evaluates to true
    if (this.state.isClicked) {
      return (
        <div className="student-info">
          <ShowStudentInfo firstName={student.firstName}
          lastName={student.lastName} phoneNumber={student.phoneNumber} />
        </div>
        )
    }
}

为了实现您想要做的事情(我猜您希望它成为第一个return语句的一部分),您可以这样做

if ((this.props.match.params.cuid) == student.courses) {
    return (
      <p>
        <button className="students" id={student._id} onClick={this.onClick}>{student.firstName} {student.lastName}</button>
        { this.state.isClicked && <div className="student-info">
          <ShowStudentInfo firstName={student.firstName}
          lastName={student.lastName} phoneNumber={student.phoneNumber} />
        </div> }
      </p>
      )
}

请注意:这将显示学生的信息作为p标签的一部分在屏幕上的所有学生,我看不出你在哪里限制显示用户的信息为一个点击的学生。

另一方面,是否确实要将球场对象与cuid参数进行比较?

 类似资料:
  • 我正在尝试隐藏按钮div和显示一个隐藏的div后,一个表单按钮已经被点击,加上延迟提交/重定向。下面是我想出的办法,但似乎没有100%奏效。 null null 任何建议都非常感谢

  • 我有2. fxml文件(stage1.fxml和stage2.fxml)和它的控制器类。 我在第一阶段有一个按钮。打开stage2的fxml。fxml单击时,上面有以下代码: 这可以很好地打开阶段2,但是阶段1仍然是打开的,我可以直接用tab键进入它并仍然使用该阶段中的按钮,我不想这样。 我怎么能。在打开stage2之前,当我按下bInstage2时,设置stage1可见(false)。fxml?

  • 我有一个gradle项目,当我放置一个断点时,它会显示为一个红点,里面有一个勾,它就可以正常工作了。 问题是,在某些类中(在同一个项目中,而不是依赖类中),红点内部没有勾号,也没有说明为什么它不会工作。但它就是不起作用..: 到目前为止我尝试了什么: null

  • 工具提示没有隐藏,如果我们在单击时禁用了该按钮 下面是一个运行示例。https://jsfidle.net/jitangupta/e8fjd0nl/ 我正在使用bootstrap 4的基本设置

  • 本文向大家介绍JS实现点击参数面板按钮显示或隐藏数据,包括了JS实现点击参数面板按钮显示或隐藏数据的使用技巧和注意事项,需要的朋友参考一下 当报表中列出数据太多时,想通过显示按钮隐藏明细数据只显示统计数据。如下图示例,那么该如何实现呢?本文以FineReport为例,来讲述JS如何实现点击参数面板按钮显示或隐藏数据。 打开报表 在参数面板添加一个标签控件,控件名为lable,设置标签控件不可见,控

  • 我正在尝试网络抓取,我需要模拟对buttoN的点击,我已经尝试过了: 并返回此错误: selenium . common . exceptions . nosuchelementexception:消息:没有这样的元素:找不到元素:{"method":"xpath "," selector ":"//*[@ id = " CTL 00 _ CP h1 _ BtnTipoGobierno "]" }