17

We already know, how to set a defaultProps.

TestComponent.defaultProps = {
    isTest: true
};

But I often used props as object type.

In Parent,

render(){
    let sample = {
        isTest : true,
        isLoggedIn : true
    }
    return (
        <div>
            <TestComponent sample = {sample} />
        </div>
    )
}

In child component, I want to set isLoggedIn to false as default. If not set (or not passed from Parent) isLoggedIn, default value is true

But I don't know how to set defaultProps for object type

TestComponent.defaultProps = {
    sample : {
        isLoggedIn: false
    }
};

How to do this ?

2
  • This isn't possible by "traditional" means. You have to setup a function that does this check manually. I actually answered a similar question a few months back. I'll link it below. Commented Feb 10, 2017 at 8:30
  • 1
    Possible duplicate of How to typecheck properties of an object prop in React? Commented Feb 10, 2017 at 8:30

3 Answers 3

9

Your code

TestComponent.defaultProps = {
  sample : {
    isLoggedIn: false
  }
};

should work.

And for type validation (propTypes), use React.PropTypes.shape for nested object props like this:

React.PropTypes.shape({
  isLoggedIn: React.PropTypes.bool
})

See the document: https://facebook.github.io/react/docs/typechecking-with-proptypes.html#react.proptypes

Sign up to request clarification or add additional context in comments.

4 Comments

It looks type check. is there no way to set a default props in object type?
Code is work. Thanks. but I want to set a default props. Alternatively, I choose the way using state like this, In constructor this.state = { isLoggedIn : (typeof props.sample.isLoggedIn == "undefined") ? false : props.sample.isLoggedIn }
@CodinCat You can only declare values for implicit props. isLoggedIn isn't a prop but a property of sample. I'm quite sure what you are demonstrating above is not possible. Or can you make a working fiddle?
That code will only work if you not pass the "sample" prop. But if you pass it like let's say an empty object, skipping the "isLoggedIn" value, then the defaultProp will not be applied. Try it: codepen.io/anon/pen/aVoZLY?editors=1010
2

I think I had the same situation, and solved like below (it doesn't look like the best solution, but it's working):

const defaultSample = {
    isLoggedIn: false,
    isTest: true
}
let { sample = defaultSample } = props; // if doesn't have a sample, it will receive the default, if it does, will just receive the sample past by its parent
sample =  { ...defaultSample, ...sample } // first receive the default properties, than the sample to replace the existed default..

Comments

1

If I had 50 reputation I would have just commented on Breno Oliveira's answer, which I used successfully with slight modifications. Thanks Breno!

function TestComponent(props)
{
    let propsSample = {...props.sampleDefault, ...props.sample};
    const [isLoggedIn,setIsLoggedIn] = useState(propsSample.isLoggedIn);
    ...
}

TestComponent.defaultProps = {
    ...
    sampleDefault: {
        isLoggedIn: false
    }
}

As @mayid says in the comments of @CodinCat's answer, if the 'sample' object is passed into the props then the defaultProp values for sample will not be applied.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.