ReactJS Application - resiliency VS failing fast

陌路散爱 提交于 2019-11-26 23:00:59

Recently I've been told that we should not do that, that the props are what we expect from the parent and if the contract is not respected to let the component break.

Exactly, if a props in component is optional, component(who renders the actual view) should handle that, not the parent component.

However, you can have a situation where the parent should break if any of the child components contract is breaking. I can think of two possible way to handle this situation-

  1. Passing error notifier to child components, where if anything goes wrong child can report error to parent component. But this is not clean solution because if there are N child and if more than one will break(or report error) to parent, you will have no clue and will be hard to manage.[This is not effective at all but wrote here because I used to follow this when I was learning React :P]

  2. Using try/catch in parent component and not trust any child component blindly and show error messages when something goes wrong. When you are using try/catch in your all component, you can safely throw error from the components when any contract is not fulfilled.

Which approach is correct and what are the pros and cons?

IMO, the second approach(try/catch in components and throwing error when requirements are not fulfilled)is valid and will solve all the issues. While writing tests for component when props are not passed you can expect an error when loading the component.

Update

If you are using React > 16, here is the way to handle errors.

It is not correct to assign default values to .isRequred props via component defaultProps. According to the official docs:

The defaultProps will be used to ensure that this.props.name will have a value if it was not specified by the parent component. The propTypes typechecking happens after defaultProps are resolved, so typechecking will also apply to the defaultProps.

If you set default property value in Component.defaultProps, then you will never get a warning if this prop isn't provided by the parent component.

In my opinion, I will not let one or two missing attribute to break my application. React acts as presentation layer in my app and I think it is way to far that showing "Oops! There is something wrong" when I can not find a key in one object. It seems like a message from a badly-broken server with 500 status, but we know it is definitely not that wrong.

For me, I do build some rules to handle communication between render function and defaultProps:

Let's say we have an user object passed from parent:

defaultProps: {
  user: {
    avatar: {
      small: ''
    }
  }
}

in the render function

render() {
  const { user } = this.props;
  // if user.avatar is not defined or user.avatar.small is empty string or undefined then we render another component we have prepared for this situation.
  if (!user.avatar || !user.avatar.small) {
    return (
      // components
      ...
    );
  }
  // normal situation
  return (
    // components
    ...
  );
}

The example above is for string and we need different implement for other data types.

Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!