繁体   English   中英

护送破坏未定义

[英]eslint destructuring coming as undefined

我有一个组件以及相应的动作和减速器。 在操作中,当我进行API调用时,我会像这样进行调用

    export const getData = () => {
     return axios.post(url, data, headers){
               .then((response)=>{
                  const messages = response.data.map({has_favourited_message: isFavourited}) //I am doing this since ESLINT throws errors
=>({isFavourited})
    })
    return messages;
     }
    }

在组件中,我像这样使用它

class ProductView extends React.Component {
  constructor(props) {
    super(props);
    this.state = { page: 0, imageUrls: [], isFavourited: props.product.isFavourited || props.product['has_favourited_message']
  }
render(){
  return null;
}
}

在减速器中,我有

case GET_DATA:
      return {
        ...state,
        data: state.data.map((item) => {
          if (item.id !== action.payload.id) return item;
          return { ...state, ...item, has_favourited_message: action.payload.isFavourited }; //Here I don't want to add any extra key so I am using the original key only as I am dispatch the above action to multiple reducers so it would become inconsistent if I change the key
        }),
      };

我正在使用props.product.isFavourited || props.product.'has_favourited_message' props.product.isFavourited || props.product.'has_favourited_message'因为ES-Lint不允许我这样使用------ props.product['has_favourited_message'] 我相信,有一种更好的方法,无需更改reducer的键或添加注释eslint-disable-line。

我是新手。 请帮忙。

您可以随时通过hasOwnProperty('property1')检查属性是否存在,以后可以使用

props.product.hasOwnProperty('has_favourited_message') ?props.product.has_favourited_message : 'There is no favourited message' 

我希望我能帮上忙

您的地图函数语法不正确,需要先包装()才能分解地图参数

.then((response)=>{                     
     const messages = response.data.map(({has_favourited_message: isFavourited}) => ({isFavourited})
     return messages;
 })

另外,您应该编写props.product.has_favourited_message代替props.product.'has_favourited_message' ,因为props.product.'has_favourited_message'是无效的语法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM