You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

传递计算值的isMyOffer属性时,checkIfMyOffer函数返回结果异常

Troubleshooting: All 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) === -1 instead of !== -1)
  • Type mismatches: Object.keys() returns string keys, but if myOffers stores 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:35:56