[英]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"> 🔍</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 对象 ( {}
) 可以派上用场。
Convert this.state.showComponent
from boolean to object将this.state.showComponent
从布尔值转换为对象
this.state={ search: '', showComponent: {}, };
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 }, })); }
Ensure index is passed to the toggle handler确保将索引传递给切换处理程序
onClick={() => this.downtoggle(index)}
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.