我渲染RootComponent:在从mapDispatchToProps传递函数的容器组分反应/终极版应用
//RENDERING ROOT COMPONENT-------------------------------------------------------------------------------
ReactDOM.render(
<Provider store={store}>
<RootComponent />
</Provider>,
document.getElementById('app'));
//RENDERING ROOT COMPONENT-------------------------------------------------------------------------------
RootComponent仅具有一个容器,包括:
//ROOT COMPONENT----------------------------------------------------------------------------------------------------
const RootComponent =() => (
<div>
<BookListContainer />
</div>
);
//ROOT COMPONENT----------------------------------------------------------------------------------------------------
BooklistContainer:
//BOOKLIST CONTAINER-------------------------------------------------------------------------------------------------------
class BookListContainer extends Component{
componentWillMount(){
console.log('componentWillMount executing...');
() => this.props.ajaxRequestTriggeredMethod();
}
render() {
return (
<div>
<BooksList DataInputParam={this.props.books} BtnClickHandler={this.props.buttonClickedMethod} />
</div>
);
}
}
const mapStateToProps = (state) => {
return{
books: state.BooksReducer
};
};
const mapDispatchToProps = (dispatch) => {
return {
buttonClickedMethod:() => dispatch({type: 'BUTTON_CLICKED'}),
ajaxRequestTriggeredMethod:() => console.log('ajaxRequestTriggeredMethod is consoled...')
};
};
connect(mapStateToProps, mapDispatchToProps)(BookListContainer);
//BOOKLIST CONTAINER-------------------------------------------------------------------------------------------------------
所有成分都在此刻一个js文件,所以我不会导出/导入除了标准库什么...
结果:我得到“componentWillMount执行......”在控制台消息,但不得到'ajaxRequestTriggeredMethod的安慰......' MESSA GE。另外,控制台中不显示任何错误。
谢谢,@GaetanBoyals,它的工作原理!但现在我只是想说明一个方面:据我了解每个_container component_ a.k.a _smart component_“连接”其子组件商店。现在,在这种特殊情况下,什么应该被看作是一个容器组件:** ** BookListContainer **或** randomNameForComponent或两者? – vshnukrshna
没有问题,我也挣扎与连接时间长;) 至于你的问题,这将是** ** randomNameForComponent,因为[此链接(https用作声明://计算器。com/questions/43414254/component-and-container-in-react-redux)作为接受的答案,“容器组件是一个反应组件,与Redux管理的状态直接相关,也称为”智能组件'“。 另外,如果我的答案解决了问题,请您将其标记为正确的?谢谢:) –
好的,谢谢你的帮助! – vshnukrshna