Search code examples
javascriptreactjsredux

getting state from connect(mapStateToProps) > UNDEFINED


I am kind of struggling to get the state of book, when I log the props I get book: undefined.

Any tips?

import React from 'react';
import { connect } from 'react-redux';
import BookForm from './BookForm';

const EditBook = (props) => {
 console.log(props)
  return (
    <div>
      hello
    </div>
  );
};

const mapStateToProps = (state, props) => {
  return {
    book: state.books.find((book) => book.id === props.match.params.id)
  };
};

export default connect(mapStateToProps)(EditBook);

Rest of the project is on my Github: https://github.com/bananafreak/bookshelf


Solution

  • Update the Link in the BookListItem. You don't need the : before ${id}. The : is causing the problem.

    <Link to={`/edit/${id}`}><h2>{title}</h2></Link>
    

    In the EditBook component return the following

    <BookForm {...props}></BookForm>
    

    In the BookForm constructor set the state from props.book

    this.state = { ...props.book }