You need to enable JavaScript to run this app. 函数组件触发onclick更新类组件state问题(Navbar下拉按钮场景)
优惠活动
大模型
产品
解决方案
定价
更多

函数组件触发onclick更新类组件state问题(Navbar下拉按钮场景)

问题解答

可以在函数组件中触发事件更新父类组件的state,只要将类组件的更新方法通过props逐层传递给子函数组件绑定即可,你当前的报错是由多处代码写法错误导致的,修复方案如下


核心错误点&修复方案

  • 错误1:Section组件点击事件参数冲突
    你在<button className="Section" onClick={(item)=>onClick(item)}>中将点击事件对象命名为item,覆盖了遍历得到的section条目,导致传给OnbuttonClick的是事件对象而非你需要的section数据,修改为:
    <button className="Section" onClick={() => onClick(item)}>
    
  • 错误2:OnbuttonClick的setState逻辑完全错误
    你当前的写法会新增一个无关的item状态属性,并没有修改Sections数组里对应条目的open状态,正确写法是遍历原Sections数组,匹配到点击的条目后翻转open状态,其余保持不变:
    OnbuttonClick = (targetItem) => {
      this.setState(prevState => ({
        Sections: prevState.Sections.map(item => 
          item.name === targetItem.name ? {...item, open: !item.open} : item
        )
      }))
    }
    
  • 错误3:缺少基础依赖引入、JSX属性写法不规范
    代码开头没有引入React的Component,同时多处使用HTML原生class属性替代JSX规定的className,input设置了固定value没有配置onChange会触发受控组件警告,form内按钮默认type为submit会触发页面刷新,这些问题都需要同步修复。
  • 错误4:子组件调用写法不规范
    你直接用函数调用的形式渲染Section、Filter等组件,建议改为标准JSX写法,避免props传递异常。

完整修复后可运行代码

import React, { Component } from 'react'
import './Intro.css'
import {Dropdown} from 'react-bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css';

var Introbar_ = class IntroBar extends Component {
  constructor(props) {
    super(props);      
    this.state = { 
      Sections:[
        {name:'Subject',open:false},
        {name:'Partner',open:false},
        {name:'Program',open:false},
        {name:'Level',open:false},
        {name:'Availability',open:false},
        {name:'Language',open:false}
      ],
      Filters:['Courses','Programs','All']
    }   
  }
   
  OnbuttonClick = (targetItem) => {
    this.setState(prevState => ({
      Sections: prevState.Sections.map(item => 
        item.name === targetItem.name ? {...item, open: !item.open} : item
      )
    }))
  }

  render() { 
    return ( 
      <div className="Intro-Container">
        <Bg/>
        <SearchBar/>
        <DisplaySections param={this.state.Sections} onClick={this.OnbuttonClick} />
        <DisplayFilters param={this.state.Filters} />
      </div>
    );
  }
}

function DisplayFilters(params) {
  const arr = params.param;
  return(
    <div className="Filter-options">
      {arr.map((item, index) => <Filter key={index} content={item} />)}
    </div>
  );
}

function Filter({content}) {
  return(
    <button className="Filter">
      {content}
    </button>
  )
}

function DisplaySections(params) {
  const arr = params.param;
  return(
    <div className="Section-options">
      {arr.map((item, index) => <Section key={index} item={item} onClick={params.onClick} />)}
    </div>
  );
}

function Section({item, onClick}) {
  return(
    <button className="Section" onClick={() => onClick(item)}>
      {item.name}
      {item.open && <CallAddDrop name={item.name} />}
    </button>
  );
}

function Bg() {
  return(
    <div className="Bg">
    </div>
  );
}

function SearchBar() {
  return(
    <div>
      <form className="Search-bar">
        <p className="Form-text">Search Courses and Programs</p>
        <input className="input" type="text" defaultValue="Computer Science"></input>
        <button className="Button" type="button">Search</button>
      </form>
    </div>
  );
}

function CallAddDrop({name}) {
  return ( 
    <div>
      <Dropdown.Item href="#/action-1">{name}</Dropdown.Item>
    </div>
  );
}
 
export default Introbar_;

内容的提问来源于stack exchange,提问作者CuriousBoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 14:45:03