我渲染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