In this issue, I have an array of objects like:
const sampleCards = [
{userName:'user1',avatarURL:'../avatar/role11.jpg',mainPageURL:'/',isFollowed:true},
{userName:'user2',avatarURL:'../avatar/role12.jpg',mainPageURL:'/',isFollowed:true},
{userName:'user3',avatarURL:'../avatar/role13.jpg',mainPageURL:'/',isFollowed:false},
];
then I used map() to render this array:
export default function SearchBar(){
return (
<div className='searchBar'>
{sampleCards.map((result)=>{
return (
<SearchResultCard result={result}/>
);
})}
</div>
);
}
A SearchResultCard component be like:
export default function SearchResultCard(result){
const [isFollowed,setIsFollowed] = useState(result.isFollowed);
const handleFollowingClicked = ()=>{
setIsFollowed(false);
};
const handleFollowClicked=()=>{
setIsFollowed(true);
};
useEffect(()=>{console.log(result.userName)});
return (
<div className="search-result-card">
<Link to={result.mainPageURL}>
<div className="search-result-card-left">
<img src={result.avatarURL} alt={result.userName} className="search-result-img"/>
<p>{result.userName}</p>
</div>
</Link>
{isFollowed ? (
<button className="following-button" onClick={handleFollowingClicked}>Following</button>
):(
<button className="follow-button" onClick={handleFollowClicked}>Follow</button>
)}
</div>
);
}
The problem is that values in result seem not been passed to so it just ends with a 'Follow' button while other elements are not rendered