Using arrays in default props


(Chris Leong) #1

Is it okay to use arrays in defaultProps or should they just be simple values? For example, if I write:

static defaultProps = {
    squares: squares: Array(9).fill("")
};

Will that cause me any issues? I know that with Python you are told not to use arrays for default args because of mutation errors, does this happen hear as well?


(Sophie Alpert) #2

defaultProps is created once so that code would result in only one array being created, although that is fine if you avoid mutating it (which we generally recommend for all props).


(Eric Masiello) #3

I can’t think of a reason why this would be a problem so long as you follow the rule that you don’t mutate or reassign props. That is to say, never do something like this:

this.props.square = someNewValue;

Props should also be passed down (or come from defaultProps).