अरे लोगों को सही क्रम में नहीं फायरिंग विधियों के साथ यहां एक मुद्दा मिला। मैं यह नहीं समझ सकता कि this.props.history.pushState(null, '/authors');
को saveAuthor() विधि में कैसे प्रतीक्षा करें।प्रतिक्रिया राउटर - इतिहास पहले की प्रतीक्षा करता है
सहायता की सराहना की जाएगी।
import React, { Component } from 'react';
import AuthorForm from './authorForm';
import { History } from 'react-router';
const source = 'http://localhost:3000/authors';
// History Mixin Component Hack
function connectHistory (Component) {
return React.createClass({
mixins: [ History ],
render() {
return <Component {...this.props} history={this.history}/>
}
})
}
// Main Component
class ManageAuthorPage extends Component {
state = {
author: { id: '', firstName: '', lastName: '' }
};
setAuthorState(event) {
let field = event.target.name;
let value = event.target.value;
this.state.author[field] = value;
return this.setState({author: this.state.author});
};
generateId(author) {
return `${author.firstName.toLowerCase()}-${author.lastName.toLowerCase()}`
};
// Main call to the API
postAuthor() {
fetch(source, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: this.generateId(this.state.author),
firstName: this.state.author.firstName,
lastName: this.state.author.lastName
})
});
};
// Calling Save author method but the this.props.history goes first rather than this.postAuthor();
saveAuthor(event) {
event.preventDefault();
this.postAuthor();
this.props.history.pushState(null, '/authors');
};
render() {
return (
<AuthorForm
author={this.state.author}
onChange={this.setAuthorState.bind(this)}
onSave={this.saveAuthor.bind(this)}
/>
);
}
}
export default connectHistory(ManageAuthorPage)
हाँ तरह से यह पता लगाया कि ईएस 7 एसिंक विधि के लिए धन्यवाद। मैंने उस पर कुछ सामान पढ़ा और इसे नहीं मिला लेकिन कोड की आपकी कुछ पंक्तियों ने मुझे सिखाया कि यह कैसे काम करता है। चीयर्स दोस्त। – Khpalwalk