admin管理员组文章数量:1406144
I want to use the 'pare' button to toggle the pare state to true or false. Next I want to pass this pare state to pivot as props.
I am literally using the same code as in the react documentation when looking at the Toggle class. .html The only thing I changed is the name isToggleOn to pare.
When looking at the console client side I get following error every time the ponent renders:
modules.js?hash=5bd264489058b9a37cb27e36f529f99e13f95b78:3941 Warning: setState(...): Cannot update during an existing state transition (such as within
render
or another ponent's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved toponentWillMount
.`
My code is following:
class Dashboard extends Component {
constructor(props) {
super(props);
this.state = { pare: true };
this.handleClick = this.handleClick.bind(this);
}
handleClick(button) {
if (button === 'pare') {
this.setState(prevState => ({
pare: !prevStatepare,
}));
}
}
render() {
return (
<Grid>
<div className="starter-template">
<h1>This is the dashboard page.</h1>
<p className="lead">
Use this document as a way to quickly start any new project.<br />{' '}
All you get is this text and a mostly barebones HTML document.
</p>
</div>
<ButtonToolbar>
<button onClick={this.handleClick('pare')}>
{this.statepare ? 'AGGREGATE' : 'COMPARE'}
</button>
</ButtonToolbar>
<PivotTable
ready={this.props.isReady}
data={this.props.gapData}
pare={this.statepare}
/>
</Grid>
);
}
}
export default (DashboardContainer = createContainer(() => {
// Do all your reactive data access in this method.
// Note that this subscription will get cleaned up when your ponent is unmounted
const handle = Meteor.subscribe('weekly-dashboard');
return {
isReady: handle.ready(),
gapData: WeeklyDashboard.find({}).fetch(),
};
}, Dashboard));
Any advice on how to fix this?
I want to use the 'pare' button to toggle the pare state to true or false. Next I want to pass this pare state to pivot as props.
I am literally using the same code as in the react documentation when looking at the Toggle class. https://facebook.github.io/react/docs/handling-events.html The only thing I changed is the name isToggleOn to pare.
When looking at the console client side I get following error every time the ponent renders:
modules.js?hash=5bd264489058b9a37cb27e36f529f99e13f95b78:3941 Warning: setState(...): Cannot update during an existing state transition (such as within
render
or another ponent's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved toponentWillMount
.`
My code is following:
class Dashboard extends Component {
constructor(props) {
super(props);
this.state = { pare: true };
this.handleClick = this.handleClick.bind(this);
}
handleClick(button) {
if (button === 'pare') {
this.setState(prevState => ({
pare: !prevState.pare,
}));
}
}
render() {
return (
<Grid>
<div className="starter-template">
<h1>This is the dashboard page.</h1>
<p className="lead">
Use this document as a way to quickly start any new project.<br />{' '}
All you get is this text and a mostly barebones HTML document.
</p>
</div>
<ButtonToolbar>
<button onClick={this.handleClick('pare')}>
{this.state.pare ? 'AGGREGATE' : 'COMPARE'}
</button>
</ButtonToolbar>
<PivotTable
ready={this.props.isReady}
data={this.props.gapData}
pare={this.state.pare}
/>
</Grid>
);
}
}
export default (DashboardContainer = createContainer(() => {
// Do all your reactive data access in this method.
// Note that this subscription will get cleaned up when your ponent is unmounted
const handle = Meteor.subscribe('weekly-dashboard');
return {
isReady: handle.ready(),
gapData: WeeklyDashboard.find({}).fetch(),
};
}, Dashboard));
Any advice on how to fix this?
Share Improve this question edited Aug 11, 2017 at 17:54 Erazihel 7,6156 gold badges34 silver badges56 bronze badges asked Aug 11, 2017 at 17:53 ChristophChristoph 1,3772 gold badges20 silver badges38 bronze badges1 Answer
Reset to default 4The reason is this line
<button onClick={this.handleClick('pare')}>
This will call the handleClick
function while executing render
function. You can fix by:
<button onClick={() => this.handleClick('pare')}>
Or
const handleBtnClick = () => this.handleClick('pare');
...
<button onClick={this.handleBtnClick}>
...
I prefer the latter
本文标签: javascriptReactset state using button and pass it as propsStack Overflow
版权声明:本文标题:javascript - React - set state using button and pass it as props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744979766a2635744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论