Nasif Md. Tanjim
Nasif Md. Tanjim

Reputation: 3972

How to refer to `defaultProps` inside `defaultProps`?

How to refer defaultProps inside defaultProps? i.e.

work.defaultProps = {
    start_date: moment().format('YYYY-MM-DD') || '',
    start_time: this.defaultProps.start_date.format('h:mm a')
};

Upvotes: 0

Views: 61

Answers (1)

Eduard Malakhov
Eduard Malakhov

Reputation: 1114

"this" won't work for you because it refers to the context, in which the code runs, not to the object you're instantiating.

This might work

work.defaultProps = {
    start_date: moment().format('YYYY-MM-DD') || '',
    start_time: work.defaultProps.start_date.format('h:mm a')
};

However you better not rely on the order of initialization of object members.

A safer, though less elegant way to do that would be

work.defaultProps = {}
work.defaultProps.start_date = moment().format('YYYY-MM-DD') || '';
work.defaultProps.start_time = work.defaultProps.start_date.format('h:mm a');

Upvotes: 1

Related Questions