2017-06-23 81 views
1

这里是形式不能通过国家行动创造者阵营

<PostBody> 
    <Input value={this.state.title} onChange={(value) => this.onChangeTitle(value)} ref="titleInput" type="text" id="title" name="title" placeholder="Post Title" /> 
    <TextArea value={this.state.body} onChange={(value) => this.onChangeBody(value)} ref="bodyInput" id="body" name="body" placeholder="Post Content" /> 
    <ToolBar> 
    <Button disabled={!this.props.ptitle || !this.props.pbody} onClick={this.props.doSave(this.state)}>Save</Button> 
    <BackLink to="/posts">Cancel</BackLink> 
    <div style={{float:'left', marginTop:'10px'}}> 
     {backBtn} 
    </div> 
    </ToolBar> 
</PostBody> 

这里是我打电话的动作制作功能

doSave: (state) => { 
    dispatch(savePostAction(state)); 
}, 

但这导致错误

warning.js:36 Warning: setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount .

并且正在无限次地导致

Uncaught RangeError: Maximum call stack size exceeded

我甚至试着在点击时调用另一个函数,然后通过传递该函数的状态来调用doSave()。仍然得到相同的错误。请帮忙。

回答

4

在您的变体中当组件渲染时,您无需单击即可调用函数。 更改onClick={this.props.doSave(this.state)}onClick={() => this.props.doSave(this.state)}

+0

谢谢。这解决了我的问题。 :) – AeJey

1

原因是,onClick期待function,你不需要调用function,它会被调用按钮的点击。

onClick={this.props.doSave(this.state)} 

这里doSave会在没有点击的情况下调用每个渲染。

所以不是:

onClick={this.props.doSave(this.state)} 

用途:

onClick={() => this.props.doSave(this.state)}