mapStateToprops不能为props分配状态?

时间:2018-10-08 07:41:06

标签: reactjs redux

我正在使用reducer,它的sems还原可以连接到redux,但不幸的是状态不能与props连接。 我已经看到了状态值,但它与道具没有联系。有人可以帮我找到如何确定道具的状态

1-reducer_books.js
------------------------------------------------------
export default function  () {
	return [
    { title: 'testTitle1' },
    { title: 'testTitle2' },
    { title: 'testTitle3' },
    { title: 'testTitle4' },
    { title: 'testTitle5' }
	]
}
---------------------------------------------------
2-book-list.js
---------------------------------------------------
import React,{Component} from 'react';
import {connect} from 'react-redux'

 class BookkList extends Component {

  renderLis() {  
		return 
			this.props.books.map((book) => {
				return (
					<li className='list-group-item' key={book.title}> {book.title}</li>
				);
			});
  }



	render(){
		return(
			<ul className="list-group col-sm-4"> 
			  {this.renderLis()}
			</ul>

		)
	}


}


function mapStateToProps(state) {
  console.log("mapStateToPropsInfo: ", state)
  return {
    books: state.books
  
  }
  
}





export default connect (mapStateToProps) (BookkList)


--------------------------------------------------
3-app.js
-------------------------------------------------

import React, { Component } from 'react';
import BookkList from '../containers/book-list'

export default class App extends Component {
  render() {
    return (
      <div>
        <BookkList />
      </div>
    );
  }
}
------------------------------------------------
4-index.js
-------------------------------------------------
import { combineReducers } from 'redux';
import BookReducer from './reducer_books';

const rootReducer = combineReducers({

	books:BookReducer

});

export default rootReducer;

我正在使用reducer,它的sems还原可以连接到redux,但不幸的是状态不能与props连接。 我看过状态值,但它与道具没有联系。有人可以帮我找到如何确定道具的状态吗? state has value books can not get state value

1 个答案:

答案 0 :(得分:1)

您将在方法“ renderLis()”中获得props值。原因在于,mapStateToProps将存储状态作为参数/参数(由react-redux :: connect提供),用于将组件与存储状态的特定部分链接。

通过链接,我的意思是mapStateToProps返回的对象将在构造时作为props提供,并且任何后续更改都可以通过componentWillReceiveProps获得。因此,如果要检查props值,可以通过在控制台上登录“ componentWillReceiveProps”生命周期方法或在呈现组件的renderLis()中进行检查。

相关问题