从父项传递数据时为什么数据未定义?

时间:2018-03-13 09:32:01

标签: reactjs redux react-redux

编辑我的假名单我正在使用axios.put。我从动作中获取数据并将其传递给我的表单组件,但所有数据都是未定义的。 我从商店获取一本书并将其传递给形成组件,并在构造函数中使用它时,它是未定义的。我觉得它很好,但现在却不是。 寻求帮助

//actions
export const EDIT_BOOK = 'EDIT_BOOK';
export const editBook = (id, values) => {
  const request = axios.put(`${ROOT_URL}/${id}`, values);
  return {
    type: EDIT_BOOK,
    payload: request
  }
};
export const FETCH_ONE_BOOK = 'FETCH_ONE_BOOK';
export const fetchOneBook = (id) => {
  const request = axios.get(`${ROOT_URL}/${id}`);
  return {
    type: FETCH_ONE_BOOK,
    payload: request
  }
};

//EidtComponent 
import React, {Component} from 'react';
import BookForm from "./BookForm";
import {editBook, fetchOneBook} from '../actions/actions_books';
import {connect} from "react-redux";

class BookEdit extends Component {
  componentDidMount = () => {
    this.props.fetchOneBook(this.props.match.params.id);
  };
  render() {
    return (
      <div>
        <BookForm
          book={this.props.book}
          onSubmit={(book) => {
            this.props.editBook(book).then(()=>{
              this.props.history.push('/')
            })

          }}
        />
      </div>
    );
  }
}
const mapStateToProps = (state) => {
  console.log('BookEdit Page: ',state.books.book);
  return {book: state.books.book}
};
export default connect(mapStateToProps, {editBook, fetchOneBook})(BookEdit);

//form component
class BookForm extends Component {
  constructor(props) {
    super(props);
    console.log(props);
    this.state = {
      userId: props.book ? props.book.userId : 0,
      title: props.book ? props.book.title : '',
      body: props.book ? props.book.body : ''
    };
  }

0 个答案:

没有答案