我是新来的反应,我很难获取单一书籍的数据超出列表,通过axios'get方法传递。 我认为这与网址有关,但我一直无法修复它。提取没有返回数据作出反应
这里是我的代码:
export function loadBook(book){
return dispatch => {
return axios.get('http://localhost:3000/api/books/book/:id').then(book => {
dispatch(loadBookSuccess(book.data));
console.log('through!');
}).catch(error => {
console.log('error');
});
};
}
//also tried this
export function loadBook(id){
return dispatch => {
return axios.get('http://localhost:3000/api/books/book/' + {id}).then(book => {
dispatch(loadBookSuccess(book.data));
console.log('through!');
}).catch(error => {
console.log('error');
});
};
}
<div className="container">
<h3><Link to={'/book/' + book._id}> {book.title}</Link></h3>
<h5>Author: {book.author.first_name + ' ' + book.author.family_name}</h5>
<h4>Summary: {book.summary}</h4>
<BookGenre genre={genre} />
</div>
链接路线
HTML代码:
<Route path="/book/:id" component={BookPage} />
编辑:代号为表成分
class BookPage extends React.Component{
render(){
const book = this.props;
const genre = book.genre;
console.log(book);
return(
<div>
<div>
<h3> {book.title}</h3>
<h5>Author: {book.author.first_name + ' ' + book.author.family_name}</h5>
<h4>Summary: {book.summary}</h4>
<BookGenre genre={genre} />
</div>
</div>
);
}
}
BookPage.propTypes = {
book: PropTypes.object.isRequired
};
//setting the book with mapStateToProps
function mapStateToProps (state, ownProps){
let book = {title: '', author: '', summary: '', isbn: '', genre: []};
const bookid = ownProps.params._id;
if(state.books.length > 0){
book = Object.assign({}, state.books.find(book => book.id));
}
return {
book: book
};
}
function mapDispatchToProps (dispatch) {
return {
actions: bindActionCreators(loadBook, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(BookPage);
首先检查这个api是否正在工作'http:// localhost:3000/api/books/book /'+ {id} fine.Next从哪里调用函数loadBook() 。 – pritesh
那个id是什么,是一个对象还是一个字符串,如果它的一个字符串不需要使用花括号。 –
@pritesh,api正在工作。使用邮差测试它。动作loadBook()充当api调用和redux thunk。这是它应该调用的路由:*** router.get('/ books/book /:id',book.book_detail); *** – Nnanyielugo