Reactjs - componentWillMount setState导致对已卸载组件的状态更改

时间:2017-02-15 16:10:53

标签: javascript reactjs

我有以下代码:

componentWillMount() {
   this.props.actions.A().then(result => {
        this.setState({ a: result });
    });
}

现在发生的事情是,如果我快速切换页面,我会收到警告:setState(...): Can only update a mounted or mounting...

我可以做些什么来阻止这种情况? (显然使用IsMounted属性可以解决它,但作为一个阅读它已过时)

1 个答案:

答案 0 :(得分:0)

尝试使用import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import java.math.BigInteger; public class CreateWordTableColumnWidth { public static void main(String[] args) throws Exception { XWPFDocument document= new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText("The Body:"); paragraph = document.createParagraph(); XWPFTable table = document.createTable(1, 2); //values are in unit twentieths of a point (1/1440 of an inch) table.setWidth(5*1440); //should be 5 inches width //create CTTblGrid for this table with widths of the 2 columns. //necessary for Libreoffice/Openoffice to accept the column widths. //first column = 2 inches width table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440)); //other columns (only one in this case) = 3 inches width for (int col = 1 ; col < 2; col++) { table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3*1440)); } //set width for first column = 2 inches CTTblWidth tblWidth = table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW(); tblWidth.setW(BigInteger.valueOf(2*1440)); //STTblWidth.DXA is used to specify width in twentieths of a point. tblWidth.setType(STTblWidth.DXA); //set width for second column = 3 inches tblWidth = table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW(); tblWidth.setW(BigInteger.valueOf(3*1440)); tblWidth.setType(STTblWidth.DXA); XWPFTableRow tableRowOne = table.getRow(0); tableRowOne.getCell(0).setText("CLientID"); tableRowOne.getCell(1).setText("CID001"); //create second row XWPFTableRow tableRow2 = table.createRow(); tableRow2.getCell(0).setText("AccountID"); tableRow2.getCell(1).setText("ACCID001"); paragraph = document.createParagraph(); document.write(new FileOutputStream("CreateWordTableColumnWidth.docx")); document.close(); } } 生命周期钩子。所以它会是

componentDidMount

这是FB所说的: &#34;在装入组件后立即调用componentDidMount()。需要DOM节点的初始化应该放在这里。如果需要从远程端点加载数据,这是实例化网络请求的好地方。在此方法中设置状态将触发重新渲染。&#34;