Javascript 我正在使用贴图渲染数组,但它总是给出最后一个元素
我正在使用map渲染广告,但当我使用Javascript 我正在使用贴图渲染数组,但它总是给出最后一个元素,javascript,arrays,reactjs,loops,Javascript,Arrays,Reactjs,Loops,我正在使用map渲染广告,但当我使用this.setRedirect重定向时,它总是给出最后一个元素,我如何将动态广告赋予RenderDirect函数,我有任何解决方案,其他解决方案对我不起作用 import React,{Component}来自“React”; 从“/Header”导入标题; 从“react router dom”导入{重定向,链接} 从“/SubmitAd/Properties/PropertiesView”导入PropertiesView; 从“./utils/stor
this.setRedirect
重定向时,它总是给出最后一个元素,我如何将动态广告赋予RenderDirect函数,我有任何解决方案,其他解决方案对我不起作用
import React,{Component}来自“React”;
从“/Header”导入标题;
从“react router dom”导入{重定向,链接}
从“/SubmitAd/Properties/PropertiesView”导入PropertiesView;
从“./utils/storage”导入{getFromStorage,setInStorage};
从“./Loader”导入加载程序;
从“./SecureHeader”导入SecureHeader;
从“/Desc”导入Desc;
常量url=`http://localhost:8080/api/`;
导出默认类仪表板扩展组件{
建造师(道具){
超级(道具);
此.state={
孤岛加载:是的,
令牌:“”,
用户名:“”,
广告:'',
值:“”,
majorCategory:“,
类别:“”,
dom:,
重定向:false
}
}
组件willmount(){
const obj=getFromStorage('olx');
if(对象和对象令牌){
const{token,username}=obj;
//在此验证令牌
获取(url+“验证”令牌=“+令牌)
.then(res=>res.json())
。然后(json=>{
if(json.success){
这是我的国家({
令牌:令牌,
用户名:用户名,
孤岛加载:false
});
}否则{
这是我的国家({
孤岛加载:false
});
}
})
}否则{
这是我的国家({
孤岛加载:false
});
}
}
注销=()=>{
这是我的国家({
孤岛加载:正确
});
const obj=getFromStorage('olx');
if(对象和对象令牌){
const{token}=obj;
//在此验证令牌
获取(url+“注销”令牌=“+令牌)
.then(res=>res.json())
。然后(json=>{
if(json.success){
这是我的国家({
令牌:“”,
孤岛加载:false
});
}否则{
这是我的国家({
孤岛加载:false
});
}
})
}否则{
这是我的国家({
孤岛加载:false
});
}
}
handleChangeCategory=(e)=>{
this.setState({[e.target.name]:e.target.value});
}
值=(e)=>{
返回此.state.type;
}
handleChange=(e)=>{
this.setState({[e.target.name]:e.target.value});
如果(e.target.value==='Properties'){
这是我的国家({
dom:
选择广告类型
全部展示
供出租
供出售
新项目
});
}否则,如果(e.target.value==='Cars'){
这是我的国家({
dom:
汽车
});
}
}
FetchAdsFromDatabase=(主要类别,类别)=>{
const obj=getFromStorage('olx');
if(obj&&obj.userId){
const{userId}=obj;
//从数据库中获取广告
获取(url+“仪表板”{
方法:“POST”,
模式:“cors”,
标题:{
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
},
正文:JSON.stringify({
userId:userId,
类别:类别,,
majorCategory:majorCategory
})
})
.then(res=>res.json())
。然后(json=>{
这是我的国家({
ads:json.ads,
})
})
}
}
showAds=(e)=>{
if(this.state.majorCategory=='Properties'&&this.state.category!=='all'){
this.fetchadsfromdatabase(this.state.majorCategory,this.state.category);
}else if(this.state.majorCategory===='showall'){
this.fetchadsfromdatabase(“showall”,null);
}else if(((this.state.majorCategory=='Properties'&&this.state.category=='all'))|(this.state.majorCategory=='Properties'))|((this.state.majorCategory=='Cars'&&this.state.category=='all')|(this.state.majorCategory=='Cars')){
if(this.state.majorCategory==='Properties'){
这个.fetchAdsFromDatabase(“属性”、“全部”);
}else if(this.state.majorCategory==='Cars'){
这个.fetchadsfromdatabase(“Cars”、“all”);
}
}else console.log(“Kuch选择karle”);
}
//属性视图=(广告)=>{
//
// }
setRedirect=()=>{
这是我的国家({
重定向:true
})
}
RenderDirect=(键)=>{
if(this.state.redirect){
返回
}
}
render(){
const{isLoading,token,username,dom,ads}=this.state;
如果(孤岛加载){
返回();
}
如果(令牌){
返回(
选择广告类别。。。
性质
汽车
电子及电器
家具
this.state = {
isLoading: true,
token: '',
username: '',
ads: '',
value: '',
majorCategory: '',
category: '',
dom: <div></div>,
redirect: false,
redirectKey: ''
}
setRedirect = (key) => {
this.setState({
redirect: true,
redirectKey: key
})
}
renderRedirect = () => {
if (this.state.redirect) {
return <Redirect to={{
pathname: '/view/' + this.state.redirectKey,
state: { referrer: this.state.redirectKey }
}} />
}
}
<button onClick={() => this.setRedirect(ad._id)} type="button" className="btn btn-outline-primary" data-toggle="modal" data-target="#exampleModal">See Details</button>