๋ฐฉ๊ธ dva์ ์ฐ๋ฝํ๊ธฐ ์์ํ๋๋ฐ dva์์ ํ๋ ๋ผ์ดํ ์ฌ์ดํด ์ด๋ฒคํธ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ ๋ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ์๋ฅผ ๋ค์ด์ฃผ์๊ฒ ์ต๋๊น?
๋ํ ์ปดํฌ๋ํธ์ ์ปดํฌ๋ํธ ๊ธฐ๋ฅ์ด React.Component๋ฅผ ํ์ฅํ๋๋ก ๋ณ๊ฒฝ๋๋ฉด ์ด๋ค ์ํฅ์ด ์์ต๋๊น?
์ปดํฌ๋ํธ์ ์ปดํฌ๋ํธ ๊ธฐ๋ฅ์ด React.Component๋ฅผ ํ์ฅํ๋๋ก ๋ณ๊ฒฝ๋๋ฉด ์ด๋ค ์ํฅ์ด ์์ต๋๊น?
ํจ๊ณผ๊ฐ ์์ต๋๋ค. ์ฌ์ ํ ์ด์ ์ต๊ด์ ๋ฐ๋ผ ์์ฑํ ๋ค์ ๋ฐ์ดํฐ ๊ณ์ธต์ dva๋ฅผ ์ถ๊ฐํ์ญ์์ค.
๊ทธ๋ฐ ๋ค์ ๋ฐ์ดํฐ ๋ ์ด์ด์ dva๋ฅผ ์ถ๊ฐํ์ธ์??? โโ๊ฐ์
๋ฐฉ๋ฒ์?
์ด์ ฏ๋ฐค์ dva+andMobile์ ์์ํ๋ ๋ฐฉ๋ฒ์ ์์๋์ต๋๋ค.
https://github.com/sorrycc/blog/issues/18 ์ด ํํ ๋ฆฌ์ผ์ ๋ฐ๋ผ ์งํํ๊ณ ์ดํดํ๋ ค๊ณ ๋ ธ๋ ฅํ์ญ์์ค.
์๊ฐ์ ๋ค์ ์ฝ๊ณ ์ ๋ณด๋ ์ข ์ฐพ์๋ดค๋๋ฐ ๊ฐ์ฅ ํฐ ๋ฌธ์ ๋ redux์ ๋ํ ์ดํด์
๋๋ค.
componentWillMount() ๋ฐ componentDidMount()์ ์ฐ๊ธฐ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ํน์ ํฌ์ธํฐ๋ฅผ ์ค ์ ์์ต๋๊น?
componentWillMount()๊ฐ ๋ผ์ฐํ
์ ํํฌ์ ๊ธฐ๋ก๋ฉ๋๊น?
componentDidMount()๊ฐ ์ธ๋ถ ๊ตฌ์ฑ ์์์ ๊ธฐ๋กํฉ๋๊น?
๊ฐ์ ์ง๋ฌธ
@xttianma @CodeSuPrMan ์ด ์ํ ๋น์ ์ฅ ๊ตฌ์ฑ ์์๋ฅผ React.Component๋ก ๋ณ๊ฒฝํ์ต๋๋ค.
https://facebook.github.io/react/docs/components-and-props.html ์ฐธ์กฐ
๋น์ ์ ์ฌ๊ตฌ์ฑ์ ๋ณผ ์ ์์ต๋๋ค
๋ ์
๋ชจ๋ฐ์ผ: +86118918960666(์ค๊ตญ)
์์ฑ:nick_zheng88
์ด๋ฉ์ผ: [email protected]
2017๋ 1์ 18์ผ ์คํ 4์ 16๋ถ์ CodeSuPrMan [email protected]์์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
์ด๊ฒ์ ์๋ ์ํ ๋น์ ์ฅ ๊ตฌ์ฑ ์์์ ๋๋ค.
const LeftNavBefore=({menus,dispatch})=>{ const{ ๋ชจ๋,ItemTree
}=props.menus; const constItemTree=ItemTree; const
NavMenu=LoadMenuTree(ItemTree,ItemTree); ํจ์ ๋ณ๊ฒฝ ๋ชจ๋(๊ฐ){
๋์คํจ์น({ ์ ํ:'๋ฉ๋ด/๋ณ๊ฒฝ ๋ชจ๋', ํ์ด๋ก๋:{ ๋ชจ๋:๊ฐ?'์์ง':'์ธ๋ผ์ธ',
์์ดํ ํธ๋ฆฌ: ์์ดํ ํธ๋ฆฌ} }) ....}
๋ผ์ดํ ์ฌ์ดํด ๊ธฐ๋ฅ์ ์ถ๊ฐํด์ผ ํ๋ ์ปดํฌ๋ํธ๋ฅผ ํ์ด ๋ ๋ฒจ ์ปดํฌ๋ํธ ํํ๋ก ํจํค์งํ๊ณ ์ถ์ต๋๋ค. ),
์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๋ชจ๋ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์๋ ์ฌ์ ํ ์ํ ๋น์ ์ฅ์ด๋ฉฐ ์๋ช ์ฃผ๊ธฐ์ ๊ท ์ผํ ํจํค์ง์ด ํ์ํฉ๋๋ค.
ํจ์ wapperComponentsStatus({ref}) {Return CompposedComponent=>{
๋ฐํ ํด๋์ค wapper๋ React.Component {componentWillMount(){๋ฅผ ํ์ฅํฉ๋๋ค.
... (๋์ค์ ์ถ์ถ๋จ)} componentDidMount(){ ... (๋์ค์ ์ถ์ถ๋จ)} render(){ ๋ฐํ}}}} `let LeftNav=ref=>wapperComponentsStatus({ref:ref})(LeftNavbefore);
์ฌ๊ธฐ์ ์ฐ๊ฒฐํ๋ ค๋ฉด ์ํ๋ฅผ ์ ๋ฌํ๊ณ ๋์คํจ์นํ ๋ชจ๋ธ๊ณผ ์ฐ๊ฒฐํด์ผ ํฉ๋๋ค.
ํจ์ mapStateToProps({๋ฉ๋ด}){
๋ฐํ {๋ฉ๋ด};
}๋ด๋ณด๋ด๊ธฐ ๊ธฐ๋ณธ ์ฐ๊ฒฐ(mapStateToProps)(LeftNav);`
ํ์ง๋ง ์ด๋ฐ ์์ผ๋ก ์์ฑํ๋ฉด ์ค๋ฅ๊ฐ ๋ณด๊ณ ๋ฉ๋๋ค. react์ redux์ ๋ํ ์ดํด๊ฐ ๋๋ฌด ๊น์ง ์๊ธฐ ๋๋ฌธ์ ์ ์๊ฐ์ ๋ฐ๋ผ๊ฐ๋ ๋ ๊น์?
wrapperComponentsStatus์ props๋ฅผ LeftNavBefore ๊ตฌ์ฑ ์์์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ(์ฃผ๋ก ์ํ ๊ธฐ๋ฅ์ ์ ๋ฌํ๊ธฐ ์ํด) ๋ฐ redux
์ฐ๊ฒฐ ํ ์ ๊ณต๋ ์ํ ๋ฐ ๋์คํจ์น๋ LeftNavBefore ๊ตฌ์ฑ ์์์ ์ ๋ฌ๋ฉ๋๋ค.โ
๋น์ ์ด ๋๊ธ์ ๋ฌ์๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ฐ๋ ๊ฒ์ ๋๋ค.
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ณ GitHub์์ ํ์ธํ์ธ์.
https://github.com/dvajs/dva/issues/506#issuecomment-273411035 ๋๋ ์์๊ฑฐ
์ค๋ ๋
https://github.com/notifications/unsubscribe-auth/AAbhwGIy_VXKLgjZAouvWgWg0i3aF6cGks5rTcpmgaJpZM4LkSJ0
.
recompose๋ฅผ ๋ณผ ์ ์์ต๋๋ค. ์ด์ ์ ๋ผ์ดํ ์ฌ์ดํด์ ๊ฐ์ธ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ง๊ธ ํ์์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด๊ฒ์ ํธ์ง์์ ์๋ ์ํ ๋น์ ์ฅ ๊ตฌ์ฑ ์์์ ๋๋ค.
const LeftNavbefore=({menus,dispatch})=>{
const s=props;
const{
mode,ItemTree
}=props.menus;
const constItemTree=ItemTree;
const NavMenu=LoadMenuTree(ItemTree,ItemTree);
function changeMode(value){
dispatch({
type:'menus/changeMode',
payload:{
mode:value?'vertical':'inline',
ItemTree:ItemTree
}
})
}
return(...)
๋ผ์ดํ ์ฌ์ดํด ๊ธฐ๋ฅ์ ์ถ๊ฐํด์ผ ํ๋ ์ปดํฌ๋ํธ๋ฅผ ํ์ด ๋ ๋ฒจ ์ปดํฌ๋ํธ ํํ๋ก ํจํค์งํ๊ณ ์ถ์ต๋๋ค. ์ด ๋ฐฉ๋ฒ), ๋ชจ๋ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์๊ฐ ์ฌ์ ํ ์ํ ๋น์ ์ฅ ์ํ๋ก ์ ์ง๋๋๋ก ํ๋ ค๋ฉด ์๋ช ์ฃผ๊ธฐ์ ํตํฉ ํจํค์ง์ด ํ์ํฉ๋๋ค.
function wapperComponentsStatus({ref}) {
return ComposedComponent=>{
return class wapper extends React.Component {
componentWillMount(){
...๏ผไนๅๆๅๅบๆฅ๏ผ
}
componentDidMount(){
...๏ผไนๅๆๅๅบๆฅ๏ผ
}
render(){
return <ComposedComponent {...this.props} />
}
}
}
}
let LeftNav=ref=>wapperComponentsStatus({ref:ref})(LeftNavbefore);
function mapStateToProps({menus}){
return {menus};
}
export default connect(mapStateToProps)(LeftNav);
๊ทธ๋ฌ๋ ์ด๋ ๊ฒ ์์ฑํ๋ฉด ์ค๋ฅ๊ฐ ๋ณด๊ณ ๋ฉ๋๋ค. ๋ฐ์ ๋ฐ redux์ ๋ํ ์ดํด๊ฐ ๋๋ฌด ๊น์ง ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ ์๊ฐ์ ๋ฐ๋ฅด๋์ง, wrapperComponentsStatus์ props๋ฅผ LeftNavBefore ๊ตฌ์ฑ ์์์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ(์ฃผ๋ก ์ํ ๊ธฐ๋ฅ์ ์ ๋ฌํ๊ธฐ ์ํด) ๋ฐ redux ์ฐ๊ฒฐ ์ํ ๋ฐ ๋์คํจ์น๋ LeftNavBefore ๊ตฌ์ฑ ์์์ ์ ๋ฌ๋ ํ ์ ๊ณตํฉ๋๋ค.
๋น์ ์ ๊ทธ๊ฒ์ ์ค์ค๋ก ํ ์ ์์ต๋๋ค
@nickzheng ๊ณ ๋ง์, ๋ณด์
๋๋ ์์ด๋ฅผ ๋ชปํด ์ฑ์ํ ๊ณํ์ ๊ธฐ๋ค๋ฆด ์ ๋ฐ์ ์๋ค ํ์ฌ ๋ฐฉ๋ฒ์
๋ผ์ฐํฐ์ ์ญํ ์ ๋ชจ๋ธ๊ณผ ๋ทฐ๋ฅผ ์ฐ๊ฒฐํ ๋ค์ React.Component๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑํ๋ ๊ฒ์
๋๋ค.
๊ทธ ์ค ํ์ฅ ๋ฐ ํ์๋ this.props๋ ์์์ ์ด๋ฉฐ ๊ฑฐ๊ธฐ์ ๋ฌด์์ด ์๋์ง ๋ช
ํํฉ๋๋ค. ์ด ํ์ด์ง๊ฐ ๋ณ๊ฒฝ๋์ง ์์ผ๋ฉด ์ง์ ํ ๋น๋ฉ๋๋ค.
import React from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import styles from './Page.css';
import { TabBar, Icon } from "antd-mobile";
import MyTabbar from './components/MyTabbar'
class Home extends React.Component {
render() {
const {menu,selectedTab,dispatch}= this.props;
const MyTabbarProps={ menu, selectedTab, dispatch }
return (
<div className="home">
{this.props.children}
<MyTabbar {...MyTabbarProps} />
</div>
);
}
}
Home.propTypes = {
menu:React.PropTypes.array,
selectedTab:React.PropTypes.number,
};
function mapStateToProps({home}) { return home }
export default connect(mapStateToProps)(Home);
์ฌ๊ตฌ์ฑ์ ์ฝ์์ง๋ง ๊ฒฐ๊ตญ ๋ด๊ฐ ์ง์ ์์ฑํ์ต๋๋ค.
import React, { Component,PropTypes } from 'react';
function wapperComponentsLifecycle({DidMount}) {
return ComposedComponent=>{
return class Wapper extends React.Component {
componentWillMount(){
}
componentDidMount(){
DidMount({props:this.props});
}
...่ฟๅฏไปฅๆทปๅ ๅซ็็ๅฝๅจๆๅฝๆฐ
render(){
return <ComposedComponent {...this.props} />
}
}
}
}
export default wapperComponentsLifecycle;
๋ค์์ ๋ชจ๋ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์๋ฅผ ์ ์งํ๊ฑฐ๋ ์ํ๋ฅผ ์ ์ฅํ์ง ์๋ ํธ์ถ์ ๋๋ค. ์ด ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๋ํํด์ผ ํฉ๋๋ค.
function DidMount({props}) {
const{ dispatch}=props;
dispatch({
type:'menus/query',
})
}
let LeftNav=wapperComponentsLifecycle({DidMount})(LeftNavbefore);
dva ๋ฌธ์๋ฅผ ๋ค์ ๋ณด๋ฉด ์ด ๋จ๋ฝ์ด ์์ต๋๋ค.
ๆไปฅๅจ dva ไธญ๏ผ้ๅธธ้่ฆ connect Model็็ปไปถ้ฝๆฏ Route Components๏ผ็ป็ปๅจ/routes/็ฎๅฝไธ๏ผ่/components/็ฎๅฝไธๅๆฏ็บฏ็ปไปถ๏ผPresentational Components๏ผ
์ด ๊ตฌ์ ์ ๋ํด ์ดํดํ๊ณ ์์ต๋๋ค. ๋ผ์ฐํฐ๋ ๋ฐ์ดํฐ/๊ตฌ์ฑ ์์ ๋ฑ์ ์์งํ๋ Route ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํฉ๋๋ค. ๊ตฌ์ฑ ์์๋ ์์ ๊ธฐ๋ฅ ๊ตฌ์ฑ ์์์ด๊ณ ์์ ๊ตฌ์ฑ ์์๋ ์๋ช ์ฃผ๊ธฐ ์ด๋ฒคํธ๊ฐ ์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
https://github.com/acdlite/recompose/blob/master/docs/API.md#lifecycle