17495

How do I set default props for a component wrapped in a Raley container?

Question:

I have a component wrapped in a Relay container:

const Widget = (props) => { return ( <div> { props.foo.bar } </div> ) } Widget.defaultProps = { foo: {bar: 0} } export default Relay.createContainer(Widget, { fragments: { store: () => Relay.QL` fragment on WidgetData { foo { bar } } ` } })

I'm using a GraphQL-as-a-service provider for a backend, and when the foo property of WidgetData isn't present, it returns null, which throws when my component attempts to render props.null.bar

As far as I know, defaultProps only works when the prop is undefined, not when it's null.

How can I protect against null results with defaultProps in this case?

Hoping to avoid writing a guard statement for each prop I reference, if possible.

Answer1:

You can do a sanity check for props from Relay container.

render() { let { foo, ...other } = this.props; let bar = foo ? foo.bar : ''; return ( <div> { bar } </div> ) }

For more info, checkout: <a href="https://github.com/bfwg/relay-gallery" rel="nofollow">https://github.com/bfwg/relay-gallery</a>

Recommend

  • Camel + MyBatis + Apache Aries
  • How to get started with dockerode
  • SMB join not working over Hive Tables
  • Avoiding N+One selects and Invalid results from eclipselink with batch read
  • Using “where in” in spring-jdbc
  • Spring MVC 404 erro http request
  • How recursion takes place in this code snippet?
  • Select more checkbox by value or id
  • grunt-init template conditional copy files
  • How to create listeners with javascript
  • How to proxy a call to a hashmap without losing the type
  • Use Unicode text with seaborn
  • Mediawiki API error: Unrecognized value for parameter 'prop': extracts
  • grant permissions in hive does not work on hdp2.2
  • Using tablesorter custom parser only for filtering with Checkboxes - working example needed
  • Omitting XML Declaration when invoking Transformer with StAXResult
  • Generic `vendors` mixin
  • Is Android's FragmentTransaction.commit() Method Thread Safe?
  • Keeping DRY between Javascript and CSS
  • How to denormalize nested list in R?
  • Running a Service from ASyncTask or Thread?
  • Majority function in SQL
  • Back press fragments issue in android
  • React Native + Redux: What's the best and preferred navigation?
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • get passwords from chrome
  • Can I programmatically choose the Android layout folder?
  • PHP CURL timing out but CLI CURL works
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • How do I change content of ComboFieldEditor?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • GridView Sorting works once only
  • SQL merge duplicate rows and join values that are different
  • Add sale price programmatically to product variations
  • Observable and ngFor in Angular 2