SharePoint Framework-在属性更改时触发ComponentDidMount

时间:2020-05-18 16:50:19

标签: spfx

每当属性发生更改时,我都希望对ComponentDidMount进行更改。

这样做的原因是,这些属性被用作API调用的一部分,该API调用不会在渲染器上触发。

现在,您可以在Render上看到,我正在分别存储属性,并在渲染上触发对我的方法的显式调用。

我敢肯定有更好的方法。

componentWillMount() {
    this.updateState();
    console.log("componentWillMount tabs");
  }


updateState()
{

console.log("updateState content: " + this.props.listTitle );

if (!this.props.listTitle)
  return;

  console.log("updateState content 2: " + this.props.listTitle );

this.props.context.spHttpClient.get("/_api/Lists/GetByTitle('" + this.props.listTitle + "')/Items?$select=Title,Contents",  
SPHttpClient.configurations.v1)  
.then((response: SPHttpClientResponse) => {  
  response.json().then((json: any) => { 
     this.setState({ data: json.value });
     this.lastTitle = this.props.listTitle
  });  
});  

}

render(): React.ReactElement<ITabListProps> {

  if (this.lastTitle != this.props.listTitle)
    this.updateState();

  // Rest Truncated

}

0 个答案:

没有答案