将其直接连接到这些组件中,或者在父组件中执行并传递道具。最后一个似乎更优化,您只需创建一个连接实例。你怎么看?
这里有一个例子:
注入子组件
const MenuIcon = connect()(({ dispatch }) => ( <IconButton onClick={ () => dispatch(reverseLeftMenu())} touch> <ImageDehaze /> </IconButton> )) const CreateIcon = connect()(({ dispatch }) => ( <IconButton onClick={() => dispatch(reverseCreateMenu())} touch> <ContentAdd /> </IconButton> )) const ClientIcons = connect()(({ dispatch }) => ( <div> <IconButton onClick={() => dispatch(reverseFavouriteMenu())}> <ActionFavorite/> </IconButton> <IconButton onClick={() => dispatch(reverseCartMenu())}> <ActionShoppingCart/> </IconButton> </div> )) const RightIcons = ({ isAdmin }) => ( isAdmin ? <CreateIcon/> : <ClientIcons/> ) const Header = ({ isAdmin = false }) => ( <Toolbar style={style.bar}> <ToolbarGroup float="left" firstChild> <MenuIcon/> </ToolbarGroup> <ToolbarTitle text="TUDELARTE"/> <ToolbarGroup float="right" lastChild> <RightIcons isAdmin={isAdmin}/> </ToolbarGroup> </Toolbar> ) Header.propTypes = { isAdmin: React.PropTypes.bool } export default Header
在父组件中连接(Header)
const MenuIcon = ({ dispatch }) => ( <IconButton onClick={ () => dispatch(reverseLeftMenu())} touch> <ImageDehaze /> </IconButton> ) const CreateIcon = ({ dispatch }) => ( <IconButton onClick={() => dispatch(reverseCreateMenu())} touch> <ContentAdd /> </IconButton> ) const ClientIcons = ({ dispatch }) => ( <div> <IconButton onClick={() => dispatch(reverseFavouriteMenu())}> <ActionFavorite/> </IconButton> <IconButton onClick={() => dispatch(reverseCartMenu())}> <ActionShoppingCart/> </IconButton> </div> ) const RightIcons = ({ isAdmin, dispatch }) => ( isAdmin ? <CreateIcon dispatch={dispatch}/> : <ClientIcons dispatch={dispatch}/> ) const Header = ({ isAdmin = false, dispatch }) => ( <Toolbar style={style.bar}> <ToolbarGroup float="left" firstChild> <MenuIcon dispatch={dispatch}/> </ToolbarGroup> <ToolbarTitle text="TUDELARTE"/> <ToolbarGroup float="right" lastChild> <RightIcons dispatch={dispatch} isAdmin={isAdmin}/> </ToolbarGroup> </Toolbar> ) Header.propTypes = { isAdmin: React.PropTypes.bool } export default connect()(Header)