我正在使用 React 框架创建 spfx webpart。我有渲染所有控件的方法。我有一个按钮,DOM 中有几个复选框,this.context.spHttpClient.post
当用户单击按钮时,它将使用 post 方法将数据发送到 SharePoint。我能够将数据提交到 SharePoint。但是一旦我提交了数据,我就无法重新加载 spfx webpart。我必须在不重新加载页面的情况下再次重新加载 Web 部件。我试图再次调用渲染方法。当然,它可能不是正确的方法,所以它不起作用。
问问题
3832 次
2 回答
1
“默认情况下,当你的组件的 state 或 props 发生变化时,你的组件会重新渲染。如果你的 render() 方法依赖于其他一些数据,你可以通过调用 forceUpdate() 告诉 React 组件需要重新渲染。调用 forceUpdate () 将导致在组件上调用 render(),跳过 shouldComponentUpdate()。这将触发子组件的正常生命周期方法,包括每个子组件的 shouldComponentUpdate() 方法。如果标记,React 仍然只会更新 DOM变化。通常你应该尽量避免使用 forceUpdate() 并且只从 render() 中的 this.props 和 this.state 中读取。
于 2017-12-04T08:50:32.287 回答
0
您应该使用以下方法:
- 组件DidMount()
- componentDidUpdate(prevProps, prevState, 快照)
- 组件WillUnmount()
- shouldComponentUpdate(nextProps, nextState)
更多信息在这里https://reactjs.org/docs/react-component.html。
例如,在一个项目中,我有一个按钮列表,然后单击,以便将事件发送到另一个使用该状态的组件:
左面板.tsx:
import * as React from 'react';
import { Button } from 'react-bootstrap';
import { Event } from '../interfaces/Event';
import '../components/GruposDeColaboracion.module.scss';
import '../css/leftPanel.css';
export class LeftPanel extends React.Component {
constructor(public props, public context) {
super(props, context);
}
private getAllGroups = (e) => {
this.clearState();
this.setActive('allGroups');
this.props.setEvent(Event.GET_ALL_GROUPS);
//e.stopPropagation();
}
public render():JSX.Element{
return (
<div className="list-group">
<Button
id="allGroups"
onClick={this.getAllGroups}
className="list-group-item rounded-0 list-group-item-action btn btn-default active">
Todos
</Button>
</div>
);
}
}
主面板.tsx:
import * as React from 'react';
import { LeftPanel } from '../views/LeftPanel';
import { CenterPanel } from '../views/CenterPanel';
import { IHomeState } from '../interfaces/IHomeState';
export class MainPanel extends React.Component<{}, IMainState> {
constructor(public props, public context) {
super(props, context);
this.state = {
event: null
};
}
private getEvent = (event) => {
this.setState({ event: event });
}
public shouldComponentUpdate(nextProps, nextState): boolean {
if (this.state.event != nextState.event) {
return true;
}
return false;
}
public render(): JSX.Element {
return (
<div className="row">
<div className="col-md-2" style={{ maxWidth: '250px' }}>
<LeftPanel setEvent={this.getEvent} />
</div>
<div className="col-md-10">
<CenterPanel event={this.state.event} context={this.props.context} />
</div>
</div>
);
}
}
中心面板.tsx:
export class CenterPanel extends React.Component<{}, ICenterPanelState> {
constructor(public props, public context) {
super(props, context);
}
public componentWillMount() {
this.state = {
render: <Spinner />
};
}
public componentWillReceiveProps(nextProps) {
if (nextProps.event == Event.GET_ALL_GROUPS) {
let dataForRender = 'Hello';
this.setState({
render: <ResponseHandler data = {dataForRender}/>
});
}
}
public render():JSX.Element{
return(
<div>
{this.state.render}
</div>
);
}
}
ResponseHandler.tsx :
import * as React from 'react';
export class ResponseHandler extends React.Component {
constructor(public props, public context) {
super(props, context);
}
public render():JSX.Element {
return (
<div>
{this.props.data}
</div>
);
}
}
在此示例中,您可以看到:
- 左侧面板使用 this.props.setEvent('custom event') 从 MainPanel 发送事件。
- 在主面板 private getEvent = (event) => {...} 接收事件并更改状态,在渲染方法中我有:。您可以在 CenterPanel 类中看到更改 prop 事件的 event={this.state.event}。
- 在中心面板 public componentWillReceiveProps(nextProps) {....} 接收事件并使用状态进行渲染。
于 2018-09-16T13:28:35.730 回答