Props argument in default argument of a prop #14750
Replies: 3 comments
-
|
The That means your use case is valid in principle: a default for one prop can be derived from another prop such as The important caveat is that this argument is the raw props object, not the fully resolved props object. So it is useful for checking values the parent actually passed, but you should not rely on other props already having their own defaults applied. A smaller version of the pattern would be: props: {
displayType: {
type: String,
required: true,
},
chartConfig: {
type: Object,
default(rawProps: Record<string, unknown>) {
const displayType = rawProps.displayType as string | undefined
return displayType && propMap[displayType]
? propMap[displayType].chartConfig
: {}
},
},
}So the reason Vue passes For your generated/default-wrapping approach, I would keep two rules:
Docs: https://vuejs.org/guide/components/props.html#prop-validation |
Beta Was this translation helpful? Give feedback.
-
|
The So this is valid: someProp: {
type: String,
default(rawProps) {
return rawProps.displayType === "bar" ? "bar-default" : "default";
},
}But there is an important limitation: these are raw incoming props, before Vue finishes resolving defaults and type casting. That means this pattern is OK for reading a required prop like For your specific use case, I would avoid dynamically rewriting every prop’s A safer pattern is to keep the external props simple, then derive the effective chart config in a computed value: const props = defineProps({
displayType: {
type: String,
required: true,
},
title: String,
width: Number,
options: Object,
});
const effectiveProps = computed(() => {
const defaults = propMap[props.displayType]?.props ?? {};
return {
...defaults,
title: props.title ?? defaults.title,
width: props.width ?? defaults.width,
options: {
...defaults.options,
...props.options,
},
};
});Then use This keeps Vue’s prop system responsible only for validation and input, and keeps your chart type based defaults in normal application logic. It also handles partial object overrides better, because Vue prop defaults do not deep merge objects. So the short answer is: the argument exists so a default factory can look at raw incoming props, but I would only use it for simple defaults. For chart type specific defaults, use a computed “resolved config” layer instead. |
Beta Was this translation helpful? Give feedback.
-
|
The Your use case is valid — deriving defaults from For your chart config pattern, I'd recommend using a const props = defineProps({
displayType: { type: String, required: true },
title: String,
width: Number,
// ... other props without complex defaults
});
const resolvedConfig = computed(() => {
const defaults = propMap[props.displayType]?.props ?? {};
return {
title: props.title ?? defaults.title,
width: props.width ?? defaults.width,
// ... merge each prop with its type-specific default
};
});Then use Why this is better:
The |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
props: {
prop: {
type: String,
default: function(props:Record<string, any>){}
}
}
what is the reason we have the props as an argument in default. We tried to replace the defaults of the props using another prop.
So the use case goes like this, we have a prop that determine the type of a chart and we have prop map of each type. So the type prop is used to determine the default value of the other props. So we tried to utilise the props argument in the default function to access the type prop and setting the default of the other props.
function getDefaultProp(props:Record<string, any>,name:string,defaultValue?:any):any {
let displayType = props.displayType;
}
export const props = {
displayType: { type: String, required: true },
.
.
.
}
for(let name in props) {
let value = props[name];
const def = extend({},value);
def.default =()=>undefined;
propsWithoutDefault[name] = def;
if(name ==="displayType"){
continue;
}
if (!value || !value.type) {
Logger.fatal(
Prop '${name}' type is not defined inside object. for flo chart it is mandatory to define the prop as <prop_name> : { type : <String|Number| etc>}.);continue;
}
}
Beta Was this translation helpful? Give feedback.
All reactions