render() {
let inputText=this.props.searchData
console.log(inputText);
let Inputreg = inputText.toLowerCase()
console.log(Inputreg)
console.log(this.state.Product)
let Product= this.state.Product;
let flag = 0;
return(
<div>
{ this.state.Product.map((obj,index)=> {
let productNameLow = obj.name.toLowerCase()
console.log(productNameLow)
var arr = productNameLow.split(' ');
console.log(arr)
console.log(Inputreg)
let n =(inArray(arr, Inputreg));
console.log(n)
if (n > 0) {
return (
<div className ="Product" key={index}>
<div className="card">
{<Link to ={{pathname: './ProductDescription' , state: { item: obj } }} className="card-img-top"><img src={obj.thumbBig[0]} alt="" /></Link>}
<div className="card-body">
<h4 className="card-title">{obj.name}</h4>
<p className="card-text-right"></p>
<input type="button" value="Add to Cart" onClick={e=>this.handleClick(obj.name,obj.price,obj.thumbBig[0],index)}/>
<input type="button" value="Add to Favourite" className="fav-button" id={index} onClick={e=>this.props.getFavourite(index)}/>
</div>
</div>
</div>
)
{flag = 1}// flag value is not being set to 1
}
})}
{(() => {
if(flag == 1){
return (
<div>
<h2>No such product exist. Try another search</h2>
</div>
)
}
})()} // function is self invoked , although i wanted to invoke this flag condition after my if(n>0) loop is completely iterated.
</div>
)
}
if (n> 0) 조건에 따라 검색 결과를 반환하고 플래그 값을 0으로 설정하면 플래그 값을 0으로 유지하고 0으로 초기화 한 다음 메시지 만 반환하려고합니다. 그러나 플래그 조건은 if 루프의 완료 후에 점검되어야합니다.reactjs를 사용하여 렌더링의 결과로 변수 값을 변경하는 방법은 무엇입니까?
왜 반환 외부에서 값을 유지하고 단순히이 메서드의 반환에서 길이를 확인하고 길이가 0이거나 메시지가없는 경우 배열을 렌더링합니다. . 실제로이 코드에서 향상시킬 수있는 부분이 많이 있습니다. 무엇을 성취하려고합니까? – brandNew