๐ณ HOC์ฌ์ฉํ์ฌ ๋ก์ง ๊ณตํตํ ์ํค๊ธฐ
๐ฅ ๊ธฐ๋ฅ ๊ณตํตํ ์ํฉ
์๋ ์ฌ์ง ์ฒ๋ผ "ํ ์คํธ" ๋ผ๋ ๋ฒํผ์ ๋๋ฅด๋ฉด '๋ชจ๋,๋ญ์ ๋ฆฌ' ์ '1990,2000s' validation ๊ธฐ๋ฅ์ด ์ค๋ณต์ด ๋๋ค.
์ค์ ์ค๋ฌด์์๋ ๋ก์ง์ด ์ด๊ฒ๋ณด๋ค ๋ ๋ณต์กํ๊ฒ ๋์๊ฐ์ง๋ง, ํ ์คํธ๋ฅผ ์ํด์ ๊ฐ๋จํ๊ฒ ๋น๊ฐ ์ฒดํฌ ์ ๋๋ง ๊ธฐ๋ฅ์ ๋ฃ์๋ค.
๐ฅ ์ ์ฒด์ ์ธ ๊ตฌ์กฐ
withCuration์ ํตํด validation์ ๊ธฐ๋ฅ์ props์ผ๋ก ์ ๋ฌํ๊ณ , props์ผ๋ก ์ ๋ฌ ๋ฐ์ ์ปดํผ๋ํธ๋ค์ validation์ ํตํด ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
๐ withCuration.js ์์ฑ
const withCuration = (component) => {
return {
name: "withCuration",
data() {
return {
curationInfo: {
mood: [
{
text: "๋ชจ๋",
value: 1
},
{
text: "๋ญ์
๋ฆฌ",
value: 2
}
],
peroid: [
{
text: "1990",
value: 1
},
{
text: "2000s",
value: 2
}
]
}
}
},
created() {
},
mounted() {
},
methods: {
validation(
curation = {
mood: [],
peroid: [],
moodAlert: () => { },
peroiAlert: () => { }
},
validateFn = () => { }
) {
if (curation.mood.length === 0) {
curation.moodAlert();
return false;
} else if (curation.peroid.length === 0) {
curation.peroiAlert();
return false;
}
validateFn();
}
},
render(createElement) {
return createElement(component, {
props: {
curationinfo: this.curationInfo,
validate: this.validation
},
})
}
}
}
export { withCuration }
props์์ ํ๋ ์ด์ ๊ฐ๋ค์ ๋์ ธ์ค๋ค ์ฌ๊ธฐ์๋ mood(๋ถ์๊ธฐ)์ , peroid๋ฅผ ์ ๋ฌํด์ค๋ค. ๊ทธ๋ฆฌ๊ณ validate๋ผ๋ ๊ฒ์ ์ ๋ฌํด์ฃผ๋ฉฐ, ์ด๊ฒ์ ํ๋ ์ด์ ์ด ๋น๊ฐ์ ์ฒดํฌํด์ฃผ๋ ํจ์์ด๋ค.
๐ฅ withCuration.js ์์ ๋ฐ๋๋ก ์์ฑ
curationView.vue
import { withCuration } from "@/hoc/withCuration";
import curationView from '@/views/userLayout/curationView.vue'
const routes = [
{
path: "/curation",
name: "curationView",
component: withCuration(curationView)
}
]
CurationComp.vue
import CurationComp from '@/components/CurationComp.vue';
import { withCuration } from '@/hoc/withCuration';
export default {
components: {
'curation-comp': withCuration(CurationComp)
}
}
curationView.vue์ CurationComp.vue ๋ withCuration์ ํตํด์ ์์์ ๋ฐ๋ ๋ค ๊ทธ๋ฌ๋ฉด vue-devtools์์ ์๋์ฌ์ง์ฒ๋ผ ํ์ธ์ด ๊ฐ๋ฅํ๋ค.
๐ง ์์์ปดํผ๋ํธ์์ withCuration ํจ์ ํธ์ถ
<template>
//๊ธฐ๋ฅ์ด ์ค์ํ๋ฏ๋ก, template์ ์๋ตํ๋ค.
</template>
<script>
export default {
name: 'CurationComp',
props: {
validate: {
type: Function,
default() {
return 'Default function'
}
},
curationinfo: {
type: Object,
default: () => {
return {}
}
},
},
data() {
return {
mood: [],
peroid: [],
};
},
mounted() {
},
methods: {
clickTest() {
const send = {
mood: this.mood,
peroid: this.peroid,
moodAlert: () => {
alert('๋ถ์๊ธฐ๋ฅผ ์ ํํด์ฃผ์ธ์.')
},
peroiAlert: () => {
alert('์๋๋ฅผ ์ ํํด์ฃผ์ธ์.')
}
}
//1.pros์ผ๋ก ์ ๋ฌ๋ฐ์ validate๋ฅผ ํธ์ถํ๋ค.
//2.send์๋ mood,peroid,moodAlert,peroiAlert ๋ฅผ ํตํด validate์ธ์์ ๋ฃ๋๋ค.
//2-1.moodAlert์ moodAlert์ validation์ ์คํจํ์๋ peroiAlert์ peroid๊ฐ ๋น๊ฐ์ผ๋ ๋์ํ๋ ํจ์์ด๋ค.
this.validate(send, this.alertValidate);
},
alertValidate() {
alert(`validation์ ์ฑ๊ณตํ์์ต๋๋ค.`)
}
},
};
</script>
<style scoped src="@/assets/styles/modal.css"></style>
validateํจ์๋ฅผ ํธ์ถํ๊ณ , mood,peroid,moodAlert,peroidAlert๋ฅผ ์ ๋ฌํ๋ฉด moodAlert,peroidAlert์ mood์ peroid๊ฐ ๋น๊ฐ์ด๋ฉด ๋์ํ๋ ํจ์ ์ด๋ค.
๐ซ ํ๊ธฐ
hoc๋ฅผ ํตํด ๊ธฐ๋ฅ์ ๊ณตํตํ ์์ผฐ์ง๋ง, hoc๋ ์ปดํผ๋ํธ ๊น์ด๊ฐ ๊น์ด์ง๋ค๋ ๋จ์ ์ด ์๋ค. mixin์ ์ธ๋ ค๋ ๋ฐ์ดํฐ๋ ๋ฉ์๋๊ฐ ๊ฒน์น๋ฉด ๋ฎ์ด์์ฐ๊ธฐ๊ฐ ๋๋ค. ํ๋ก์ ํธ๊ฐ ์ปค์ง์๋ก ์ด๊ฒ์ ์ถ์ฒ์ด ์ด๋ ต๊ธฐ๋๋ฌธ์, hoc๊ฐ ์ปดํผ๋ํธ ๊น์ด๊ฐ ๊น์ด์ง๋ค๋ ๋จ์ ์ด ์์์๋ ๋ถ๊ตฌํ๊ณ ์ ํํ์๋ค.