首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react- file -viewer仅在文件类型更改时重新呈现

react- file -viewer仅在文件类型更改时重新呈现
EN

Stack Overflow用户
提问于 2020-03-17 21:28:51
回答 1查看 506关注 0票数 2

我在sharepoint react-web部件中使用react-file-viewer。只要文件的后缀发生变化,它就可以正常工作。如果后缀保持不变,并且只更改了文件名,则react-file-viewer组件中不会重新呈现。

在spfx环境中有什么需要特别考虑的吗?

代码语言:javascript
复制
import * as React from 'react';
import FileViewer from 'react-file-viewer';
import { LibraryItem } from './LibraryItem';
//import { CustomErrorComponent } from 'custom-error';

export interface IDocumentViewProps {
  fileItem: LibraryItem;
  DocumentViewKey: any;
}

export default class DocumentView extends React.Component<IDocumentViewProps, any> {
  private path: string = "";
  private type: string = "";
  private fileItem: LibraryItem;

  constructor(props: IDocumentViewProps,any) {
    super(props);
    this.state = {
      path: '',
      suffix: ''
    };
  }

  private setFileComponents(item: LibraryItem) {
    if (item) {
      this.path = item.FileRef;
      this.type = item.File_x0020_Type;
      this.setState({ suffix: this.type });
      this.setState({ path: this.path });
    }
  }

  public componentWillReceiveProps(nextProps) {
    const { fileItem } = this.props;
    if (nextProps.fileItem !== fileItem) {
      this.fileItem = nextProps.fileItem;
      this.setFileComponents(this.fileItem);
    }
  }

  public render(): React.ReactElement<IDocumentViewProps,any> {
    return (
      <FileViewer
        fileType={this.state.suffix}
        filePath={this.state.path}
        // errorComponent={CustomErrorComponent}      
        onError={this.onError} 
        on
        />

    );
  }

  private onError(e) {
    // logger.logError(e, 'error in file-viewer');
    console.log(e, 'error in file-viewer');
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-02 15:26:09

解决方案(解决方法)是设置key={currentFile.id}。多亏了https://github.com/yulolimum,我在github https://github.com/plangrid/react-file-viewer/issues/142上得到了这个非常有用的提示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60723407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档