简体   繁体   中英

is it better to pass a function as a prop, or keep it outside the parent component and import?

pretty much title.

i have something like this:

const updateJson = (props) => {
    const {mainprops,userprops,publishprops,...choosebyprops} = props;
    const choicesMade = publishprops.buysideChoice;
    let updatedJson = {};  
    for ( let [id,obj] of Object.entries(publishprops.jsonData)){
        for ( let [key, val] of Object.entries(obj) ){
            if (key == 'attrs') {
                for (let [dbKey,dbVal] of Object.entries(val) ){
                    if(dbVal == choicesMade[dbKey]) {
                    }
                }
            }
        }
    }
    return(updatedJson)
    };

and something like this:

const getNextChoices = (props) => {
    const {mainprops,userprops,publishprops,choosebyprops,attrprops} = props;
    const nextAttr = attrprops.attrChoice;
    const nextFilter = attrprops.attrFilter;
    const choicesMade = publishprops.buysideChoice;
    let attrDupes = [];
    for ( let [id,obj] of Object.entries({...nextFilter})){ 
        for ( let [key, val] of Object.entries(obj) ){ 
            if (key == 'attrs') {
                for (let [dbKey,dbVal] of Object.entries(val) ){ 
                    if (publishprops.buysideChoice>2){
                        if(dbVal == choicesMade[dbKey]) {
                                attrDupes.push(key[nextAttr]);
                        }
                    }
                    else {
                        attrDupes.push(val[nextAttr]);
                    }
                }
            }
        }
    }
    let uniqueAttrs = [...new Set(attrDupes)].sort();
    return(uniqueAttrs)
    };

i've been passing updateJson and getNextChoices as props and trying to call them that way...wondering if that was not the best approach.

如果您打算在具有这些功能的组件的子组件中使用它们,因此将它们作为 props 传递是最好的选择,否则如果您打算在不同的组件或不同级别的子组件中使用它们,那么最好在外部声明它们父组件并导入它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM