传递计算值的isMyOffer属性时,checkIfMyOffer函数返回结果异常
isMyOffer Values Returning False Let’s walk through why your isMyOffer prop is always false, even though you expect the first Offer component to return true. Here are the most likely issues and how to debug them:
1. Double-check your checkIfMyOffer function logic
This function is the core of your check—if its logic is flipped or incorrect, you’ll get false across the board. Common mistakes here include:
- Accidentally checking for absence instead of presence (e.g.,
return myOffers.indexOf(key) === -1instead of!== -1) - Type mismatches:
Object.keys()returns string keys, but ifmyOffersstores numeric IDs, the string key won’t match
Example of a corrected function (assuming myOffers is an array of keys):
checkIfMyOffer = (key, myOffers) => { // Normalize types to avoid mismatches const normalizedKey = typeof key === 'string' ? Number(key) : key; return myOffers.includes(normalizedKey); }
2. Verify account.myOffers actually contains the first offer’s key
It’s easy to assume the first key is in myOffers, but let’s confirm with direct logging:
// Add this right before your return statement console.log("My offers list:", this.props.account.myOffers); console.log("First offer key:", Object.keys(offers)[0]);
If myOffers is empty, or the first key isn’t present, that’s why all checks fail. Make sure your addToMyOffers action correctly adds the first offer’s key to the account.myOffers state when it’s created.
3. Fix key type mismatches from Object.keys()
Object.keys() always returns string values, even if your object uses numeric keys. If myOffers stores numeric IDs, convert the key to the correct type before passing it to checkIfMyOffer:
isMyOffer={this.props.checkIfMyOffer(Number(key), this.props.account.myOffers)}
4. Add inline debugging to see per-key results
Insert console logs directly in your map function to see exactly what’s happening for each offer:
{Object.keys(offers).map(key => { const isMy = this.props.checkIfMyOffer(key, this.props.account.myOffers); console.log(`Offer key ${key}: isMyOffer = ${isMy}`); return ( <Offer key={key} index={key} details={offers[key]} account={this.props.account} isMyOffer={isMy} addToMyOffers={this.props.addToMyOffers} /> ) })}
This will show you if specific keys are failing, helping you narrow down whether the issue is with the function, data types, or missing data in myOffers.
Start with these steps—chances are the problem is in either the checkIfMyOffer logic or a mismatch between the key type and what’s stored in myOffers.
内容的提问来源于stack exchange,提问作者petithomme

