Username.jsx 628 B

123456789101112131415161718192021222324252627282930
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. export default class Username extends React.Component {
  4. renderForNull() {
  5. return <span>anyone</span>;
  6. }
  7. render() {
  8. const { user } = this.props;
  9. if (user == null) {
  10. return this.renderForNull();
  11. }
  12. const name = user.name || '(no name)';
  13. const username = user.username;
  14. const href = `/user/${user.username}`;
  15. return (
  16. <a href={href}>{name} (@{username})</a>
  17. );
  18. }
  19. }
  20. Username.propTypes = {
  21. user: PropTypes.oneOfType([PropTypes.object, PropTypes.string]), // Possibility of receiving a string of 'null'
  22. };