reactjs - 仅当文件类型更改时,react-file-viewer 才会重新渲染

标签 reactjs sharepoint

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

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

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');
  }
}

最佳答案

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

关于reactjs - 仅当文件类型更改时,react-file-viewer 才会重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60723407/

相关文章:

sharepoint - 网站定义中的 Web 部件连接

sharepoint - 在 SharePoint 中显示 SSRS 报告?

reactjs - [SPLoaderError.loadComponentError] : ***Failed to load component

javascript - react native : My (created) component isn't found?

javascript - 逐类导入还是整个模块导入,哪个最好?

html - 我试图在图像中间对齐文本。我不确定我在这里做错了什么

javascript - 使用 SPServices 从 Sharepoint 列表中获取项目时,项目未定义

javascript - React onSubmit 方法仅填充第二个事件

reactjs - 通过 api 调用,useEffect 中未发生状态更新

javascript - 并排显示两个列表项