Reputation: 1066
This is my piece of code, which works correctly (adds records), but throws an error after addition:
Uncaught Error: Invariant Violation: enqueueCallback(...): You called
setProps
,replaceProps
,setState
,replaceState
, orforceUpdate
with a callback that isn't callable.
handleSubmit: function(e) {
e.preventDefault();
return $.post('', {page: this.state},
function(data) {
this.props.handleNewPage(data);
return this.setState(this.getInitialState(), 'JSON');
}.bind(this)
);
}
There are no routes for now. Can someone help me to solve this?
Upvotes: 13
Views: 27614
Reputation: 1802
You actually invoke getInitialState
instead of providing a reference to the function itself on the line return this.setState(this.getInitialState(), 'JSON');
and the second argument should never be anything but a callback function.
this.setState
expects either a function that should return a state object as a single argument, or an object to merge the current state with (plus optionally a callback as a second argument to run after state has been set). So either you provide just a function that returns the new state, or you provide an object that the current state should be merged with as a first argument and a callback that will be executed after the state has been set as second argument.
Either this.setState(this.getInitialState)
or this.setState(this.getInitialState())
or this.setState(newState, callback)
.
Check out the signature in the API here.
Upvotes: 1
Reputation: 174957
The second (optional) parameter to setState
is a callback function, not a string. You can pass a function that will be executed once the operation is completed.
Upvotes: 32