无法显示值

问题描述 投票:0回答:3

下面是我在render方法中的代码,我正在尝试访问值arr[1].Title,但收到以下错误。

public render(){
   var arr;
   return (
      {
         arr = this.cardarrange(this.props.pdata[i], this.props.pdata[i+1], this.props.pdata[i+2])
      }
      {
         console.log(Object.keys(arr))
      }
      {
         console.log(JSON.stringify(arr[1].Title))
      }
      <h1>{JSON.stringify(arr[1].Title)}</h1>
   )
}

这是什么原因

错误:

对象作为React子对象无效(找到:带有键的对象{_query,_options,_url,_parentUrl,_useCaching,_cachingOptions,_cloneParentWasCaching,_cloneParentCacheOptions,_requestPipeline,_batch,_batchDependency,_forceCaching,删除,odata.metadata,odata.type,odata.id,odata.etag,odata.editLink,FileSystemObjectType,Id,ServerRedirectedEmbedUri,ServerRedirectedEmbedUrl,ContentTypeId,ComplianceAssetId,WikiField,标题,CanvasContent1,BannerImageUrl,说明,PromotedState,FirstPublishedDate,LayoutWebpartsContent,OData__AuthorBylineId,_AuthorBylineStringId,OData__TopicHeader,OData__SPSitePageFlags,OData__OriginalSourceUrl,OData__OriginalSourceSiteId,OData__OriginalSourceWebId,OData__OriginalSourceListId,OData__OriginalSourceItemId,种类,News_x0020_Categories,Read_x0020_times,Video_x0020_URL,分区,功能,群集,MCO,工作场所,Card_x0020_Type,NewsSource,Enable_x0020_Share,ID,已创建,AuthorId,已修改,EditorId,OData__CopySource,CheckoutUserId,OData__UIVersionString,GUID,LikeCount,CommentCount,FileRef})。

javascript reactjs
3个回答
0
投票

您的render应该返回一个有效值以显示,而不是计算或console.log结构。但是您可以像下面这样进行任何转换

public render(){
   var arr = this.cardarrange(this.props.pdata[i], this.props.pdata[i+1], this.props.pdata[i+2];
   console.log(Object.keys(arr))
   console.log(JSON.stringify(arr[1].Title))

   return (
      <h1>{JSON.stringify(arr[1].Title)}</h1>
   )
}

0
投票

return语句后只应具有JSX(伪HTML),将其余代码移到return之前:

public render(){
   var arr = this.cardarrange(this.props.pdata[i], this.props.pdata[i+1], this.props.pdata[i+2]);
   console.log(Object.keys(arr));
   console.log(JSON.stringify(arr[1].Title));

   return (
      <h1>{JSON.stringify(arr[1].Title)}</h1>
   )
}

此外,为<h1>输出字符串化数据可能没有意义,这可能是您需要使用的:

   return (
      <h1>{arr[1].Title}</h1>
   )

0
投票

避免使用JSON.stringify。

     public render(){

      let arr = this.cardarrange(this.props.pdata[i],this.props.pdata[i+1],this.props.pdata[i+2])};
      return (
      <h1>{ arr[1].Title}</h1>
);
}

或尝试以下方法:

public render() {
let propsData= JSON.parse(this.props.pdata);
let arr = this.cardarrange(propsData[i],propsData[i+1],propsData[i+2]);
return (
  <h1> {arr[1].Title}</h1>
);
}

© www.soinside.com 2019 - 2024. All rights reserved.