简体   繁体   English

我想在地图功能中创建一个单独的过程,比如点击切换

[英]i would like to make an individual process in a map function like toggle on click

i want to make a button toggle on each div i click in map function but the buttons toggle in whole dives on one click (i want to make each click toggle abutton on only the div that i clicked not all of them) ...................................................................................................我想在我在地图功能中单击的每个 div 上进行一个按钮切换,但是单击一次即可在整个潜水中切换按钮(我想使每次单击仅在我单击而不是全部单击的 div 上切换一个按钮).... ………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………

import React,{Component}  from 'react'
import './course.css'
import Downloadcourse from './../download/down.js'
import {Transition,animated} from 'react-spring/renderprops'

class Course extends Component{
    constructor(){
        super();
        this.state={
            search:'',
            showcomponent:false,
        };
        
    }
    updatesearch=(e)=>{
        this.setState({search:e.target.value.substr(0,20)});
    }
    
    downtoggle=(index)=>{
        
        this.setState({showcomponent:!this.state.showcomponent})
    }
  render(){
      let filteredcontacts= this.props.corses.filter(
      (item)=>{
          return item.name.toLowerCase().indexOf(
              this.state.search.toLowerCase()) !== -1 ;
          
      }) ;
      let length= this.props.corses.length;
      const courselist=length ?(
       filteredcontacts.map((item,index)=>{
         return (
       <div className="col-sm-3 cat" key={item.id}>
       <div className="maincover" >
      <div className="mainimg" onClick={this.downtoggle}>
      </div>
      <div className="maincorse">
      <div className="row course-title">
      <h1 className="course-Name">{item.name}</h1> 
      </div>
      <div className="row course-title">
      <button className="removeing" onClick={()=>{this.props.deleteing(index)}}>Remove 
      Course</button>
      </div>
      <div className="row download">
      <Transition
        native
         items={this.state.showcomponent}
          from={{ position: 'absolute', overflow: 'hidden', height: 0 }}
            enter={[{ height: 'auto' }]}
            leave={{ height: 0 }}>
            {show=>show &&(props=>(
            <animated.div style={props}>
                <Downloadcourse />          
            </animated.div>               
                           ))}
      </Transition>
      </div>
      </div>
       </div>
       </div> 
  )}
      )) :(
          <div className="no-content">
          <h2>no content to show</h2>
          </div>
         )
      return(

    <div className="course">
      <input type="text" className="input-search" onChange={this.updatesearch}/>
      <span className="magnficant"> &#128269;</span>
    <div  className="row category">
     {courselist}
</div>
    </div>
  )
}
}

export default Course;
                        
                        

To make multiple elements toggleable you need to store each element's toggled state in some data structure.要使多个元素可切换,您需要将每个元素的切换状态存储在某个数据结构中。 A javascript object ( {} ) comes in handy for this.一个 javascript 对象 ( {} ) 可以派上用场。

  1. Convert this.state.showComponent from boolean to objectthis.state.showComponent从布尔值转换为对象

     this.state={ search: '', showComponent: {}, };
  2. Use the index passed to toggle handler to update toggled state使用传递给切换处理程序的索引来更新切换状态

     downtoggle = (index) => { this.setState(prevState => ({ showComponent: { ...prevState.showComponent, // <-- spread existing state [index]: !prevState.showComponent[index], // <-- toggle value }, })); }
  3. Ensure index is passed to the toggle handler确保将索引传递给切换处理程序

     onClick={() => this.downtoggle(index)}
  4. Check the toggled state for the transition component检查转换组件的切换状态

     <Transition // ... other props items={this.state.showComponent[index]} // <-- truthy/falsey />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重置 a.click 切换 function? - How would I reset a .click toggle function? jQuery toggle()函数。 我想要一些批评或建议,以更好地实践 - jQuery toggle() function. I would like some critique or suggestions for better practice 我想在点击时设置状态(某个对象) - i would like to setState(some object) on click 我想将一个单词字符串拆分为不同的单个字母 - I would like to split a word string into different individual letters 如何切换<a>像手风琴一样工作的</a>高度<a>?</a> - How can I toggle height of <a> where it would work like an accordion? 我想让一个javascript数组成为条件 - I would like to make a javascript array conditional 我想让我的图片弹出时变大 - I would like to make my image pop out larger when I click on it 当我单击编辑图像时,我希望我的编辑功能能够正常工作 - I would like to have my edit function to work when i click on the edit image 全局变量干扰其他代码我想将它作为局部变量放入这些函数中,使其全部放入 1 个大 function - Global variable is interfering with other code I would like to put it into these functions as a local variable of make it all into 1 big function 我想用拆分方法和 map 和 javascript 中的 object 分开 - I would like to separate with split method and map an object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM