Warning.js:45警告:setState(…):只能更新已安装或安装的组件

我是js新手我试图使用this.setState({但我收到警告。你能告诉我为什么我得到以下警告警告.js:45警告:setState(…):只能更新已安装或安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是一个无操作。请检查PerformanceChartNav组件的代码。

提供以下代码: –

 import React from 'react'; import {connect} from 'react-redux'; import moment from 'moment'; import height from './jump-player-tab-content'; import weight from './jump-player-pane-content'; import AccountSetupTab from './running-setup'; import BalancePortfolio from './balance-portfolio'; class FirstTimeTab extends React.Component { constructor(props) { super(props); this.state = { runningSetup: 'jump-player-active', investmentPurchase: 'sports-invest-ico', balancePortfolio: 'sports-balance-ico', performance: 'sports-perf-ico', selectedTab: 0 }; } componentWillMount() { let {sportsPhone, runningSnapshot, runningPositions, runningPerformance} = this.props; let managedProductActivationDate = this.props.sportsPhone.managedProductActivationDate; let past1Day = moment().subtract(1, 'days').format('YYYY-MM-DD'); let isActive= managedProductActivationDate  0 ) { this.setState({ selectedTab: 2, runningSetup: "ft-prev-day", investmentPurchase: "ft-prev-day", balancePortfolio: "sports-balance-ico jump-player-active ft-active-tab" }); }else if(runningPerformance !== undefined ) { this.setState({ selectedTab: 3, runningSetup: "dft-prev-day", investmentPurchase: "ft-prev-day", balancePortfolio: "ft-prev-day", performance: "sports-perf-ico jump-player-active ft-active-tab" }); } } callback(selectedTab){ if (this.state.selectedTab !== selectedTab) { switch(this.state.selected) { case 0: let temp = this.state.runningSetup.substring(32, 45); this.setState({runningSetup: temp}); break; case 1: let temp1 = this.state.runningSetup.substring(33, 46); this.setState({runningSetup: temp}); break; case 2: let tem2 = this.state.runningSetup.substring(34, 47); this.setState({runningSetup: temp}); break; case 3: let temp3 = this.state.runningSetup.substring(31, 41); this.setState({runningSetup: temp}); break; } switch(selected) { /*case 0: let temp = this.state.runningSetup+' ft-active-tab'; this.setState({runningSetup: temp}); break;*/ case 0: let temp4 = this.state.runningSetup.substring(0, 32); this.setState({runningSetup: temp}); break; case 1: let temp5 = this.state.runningSetup.substring(0, 33); this.setState({runningSetup: temp}); break; case 2: let temp6 = this.state.runningSetup.substring(0, 34); this.setState({runningSetup: temp}); break; case 3: let temp7 = this.state.runningSetup.substring(0, 31); this.setState({runningSetup: temp}); break; } } } render () { // service changes need to be done let isActiveSecond= managedProductActivationDate < past1Day; // let {sportsPhone, runningSnapshot, runningPositions, runningPerformance} = this.props; let managedProductActivationDate = this.props.sportsPhone.managedProductActivationDate; let past1Day = moment().subtract(1, 'days').format('YYYY-MM-DD'); // let isActive= managedProductActivationDate < past1Day; //let selectedTab = 0; return (   

Once we verify your deposit and your running has been funded, we'll start purchasing ETFs for your portfolio.

You'll be able to monitor your portfolio as it responds to daily market changes.

); } } FirstTimeTab.propTypes = { sportsPhone: React.PropTypes.object.isRequired, runningSnapshot: React.PropTypes.object.isRequired, runningPositions: React.PropTypes.object.isRequired, runningPerformance: React.PropTypes.object.isRequired, portfolio: React.PropTypes.object.isRequired, managedProductActivationDate: React.PropTypes.object.isRequired }; function select(state) { return { sportsPhone: state.sportsPhone, runningSnapshot: React.PropTypes.object.isRequired, runningPositions: React.PropTypes.object.isRequired, runningPerformance: React.PropTypes.object.isRequired, portfolio: state.portfolio, past1Day: React.PropTypes.number, isActive: React.PropTypes.object.isRequired }; } export default connect(select)(FirstTimeTab);

更改:

 componentWillMount 

 componentDidMount 

必须先安装组件,然后才能执行设置状态等操作。

出现警告是因为您在componentWillMount生命周期中使用了setState。 看一下这个教程https://facebook.github.io/react/tutorial/tutorial.html ,你可以在这里找到如何正确使用setState()