2017-06-23 34 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)}