Javascript 属性更改时如何重新渲染react组件
所以我有这个react组件,它有一个下拉属性(SPFx),它有两个值,我需要当下拉列表被更改时,react再次被重新呈现,下拉列表定义了从中检索值的数据源 Webpart.tsJavascript 属性更改时如何重新渲染react组件,javascript,reactjs,sharepoint,spfx,Javascript,Reactjs,Sharepoint,Spfx,所以我有这个react组件,它有一个下拉属性(SPFx),它有两个值,我需要当下拉列表被更改时,react再次被重新呈现,下拉列表定义了从中检索值的数据源 Webpart.ts import * as React from 'react'; import * as ReactDom from 'react-dom'; import { Version } from '@microsoft/sp-core-library'; import { BaseClientSideWebPart,
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneDropdown
} from "@microsoft/sp-webpart-base";
import * as strings from 'AbstractfactoryWebPartStrings';
import Abstractfactory from './components/Abstractfactory';
import { IAbstractFactoryProps } from './components/IAbstractFactoryProps';
import { IAbstractfactoryWebPartProps } from "./IAbstractfactoryWebPartProps";
export default class AbstractfactoryWebPart extends BaseClientSideWebPart<IAbstractfactoryWebPartProps> {
public render(): void {
const element: React.ReactElement<IAbstractFactoryProps > = React.createElement(
Abstractfactory,
{
datasource: this.properties.datasource
}
);
ReactDom.render(element, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected onPropertyPaneFieldChanged(propertyPath: string, oldValue: any, newValue: any): void {
super.onPropertyPaneFieldChanged(propertyPath, oldValue, newValue);
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneDropdown("datasource", {
label: "DataSource",
options: [
{ key: "1", text: "Sharepoint"},
{ key: "2", text: "JSON" }
],
selectedKey: "1",
})
]
}
]
}
]
};
}
}
道场
import ICustomerDAO from "./ICustomerDAO";
import DataSources from "./DatasourcesEnum";
abstract class DAOFactory {
//For each entity we will need to implement getCustomerDAO, this will make it easily replaceable
//when another datasource comes in
public abstract getCustomerDAO(): ICustomerDAO;
//Static method that receives a parameter depending on the datasource and will return the specifc
//factory
public static getDAOFactory(whichFactory: DataSources): DAOFactory {
switch (whichFactory) {
case DataSources.SharepointList:
return new SharepointListDAOFactory();
case DataSources.JsonData:
return new JsonDAOFactory();
default :
return null;
}
}
}
export default DAOFactory;
import SharepointListDAOFactory from "./SharepointListDAOFactory";
import JsonDAOFactory from "./JsonDAOFactory";
JsonCustomerDao.ts
import ICustomerDao from "./ICustomerDao";
import Customer from "./Customer";
class JsonCustomerDAO implements ICustomerDao{
public insertCustomer(): number {
// implementation to be done by reader
return 1;
}
public deleteCustomer(): boolean {
// implementation to be done by reader
return true;
}
public findCustomer(): Customer {
// implementation to be done by reader
return new Customer();
}
public updateCustomer(): boolean {
// implementation to be done by reader
return true;
}
public listCustomers(): Customer[] {
// implementation to be done by reader
let c1: Customer= new Customer();
let c2: Customer= new Customer();
c1.id="3";
c1.firstName="Andrew";
c1.lastName="Valencia";
c2.id="4";
c2.firstName="Charles";
c2.lastName="Smith";
let list: Array<Customer> = [c1, c2 ];
return list;
}
}
export default JsonCustomerDAO;
但是视图没有重新渲染,我可以使用任何react js事件在道具更改时重新渲染或重新设置状态吗?在数据更改时重新渲染组件的react方法是更新其状态 例如,如果您有一个组件
,它包含一个渲染方法来显示它的状态,如
...
render() {
return(
<div>Hello {this.state.name}</div>
)
}
...
这篇文章写得很好,也做了一些解释。我建议您也研究一下一般的生命周期方法
更新
比如像这样
class Foo extends React.Component {
constructor(props){
super();
window.someFunction = payload => {
// do whatever you want to do with the payload here
this.setState({ items: payload });
}
}
}
更新2
如果您的组件正在侦听/接收道具,您可以使用
componentWillReceiveProps(newProps)
lifecycle方法,在该方法中反应并更新您的组件状态我知道,问题是这是Sharepoint Framework,其中我有一个带有一个下拉属性的Web部件,然后Web部件有react组件,有一个名为onPropertyPaneFieldChanged的事件,我可以在其中放置任何逻辑,但是如何在那里传递新状态?或者我如何触发再次执行构造函数,不确定这是否清楚我的问题。在JavaScript OOP编程中,当类实例化时,构造函数只被调用一次。所以不能再叫了。您可以做的是,在构造函数中创建一个方法,或者在ComponentDidMount中创建一个全局方法,该方法可以从外部访问。e、 g.window.something=()=>{};使用该方法,您会收到发生的任何更新,然后在构造函数中添加setState方法?不确定我是否理解了。更新了我的答案。对不起,我正在工作,有时不得不缩短它^^不确定它是否会工作属性“someFunction”在类型“Window”上不存在。
import ICustomerDao from "./ICustomerDao";
import Customer from "./Customer";
class SharepointCustomerDao implements ICustomerDao {
public insertCustomer(): number {
// implementation to be done by reader
return 1;
}
public deleteCustomer(): boolean {
// implementation to be done by reader
return true;
}
public findCustomer(): Customer {
// implementation to be done by reader
return new Customer();
}
public updateCustomer(): boolean {
// implementation to be done by reader
return true;
}
public listCustomers(): Customer[] {
// implementation to be done by reader
let c1: Customer = new Customer();
c1.id="1";
c1.firstName="Luis";
c1.lastName="Valencia";
let c2: Customer = new Customer();
c2.id="2";
c2.firstName="John";
c2.lastName="Smith";
let list: Array<Customer> = [c1, c2 ];
return list;
}
}
export default SharepointCustomerDao;
protected onPropertyPaneFieldChanged(propertyPath: string, oldValue: any, newValue: any): void {
this.properties[this.properties.datasource] = newValue;
this.render();
super.onPropertyPaneFieldChanged(propertyPath, oldValue, newValue);
}
...
render() {
return(
<div>Hello {this.state.name}</div>
)
}
...
...
foo(receivesSomething) {
const items = this.state.items;
items.push(receivesSomething);
this.setState({items}); // once a setState is being executed in any component react will make a diff between the current DOM and the ShadowDOM, if something is different then it will trigger a re-render of the affected component
}
...
class Foo extends React.Component {
constructor(props){
super();
window.someFunction = payload => {
// do whatever you want to do with the payload here
this.setState({ items: payload });
}
}
}