Guest User

Untitled

a guest
Feb 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // Challenge: Refactor the `render()` method with declare all variables at top
  2.  
  3. render() {
  4. return (
  5. <li>
  6. <div className="profile-card">
  7. <header className="profile-header" onClick={this.toggleClass}>
  8. <img src={this.props.profile.image} alt={this.props.profile.name} />
  9. <h2>{this.props.profile.name}</h2>
  10. </header>
  11. <section
  12. className={
  13. this.state.active ? "skills-container hidden" : "skills-container"
  14. }
  15. >
  16. <h4>Skills</h4>
  17. <ul className="skills-list">
  18. {this.props.profile.skills.map(skill => {
  19. return <li key={skill}>{skill}</li>
  20. })}
  21. </ul>
  22. </section>
  23. </div>
  24. </li>
  25. )
  26. }
  27.  
  28. // Refactors to ->
  29.  
  30. // 1. Replace all `this.props.profile` instances to be just `profile` to allow for readability
  31. // We use destructuring to accomplish this
  32. // Now we know where the `profile` is coming from when we are referring to it
  33. // 2. Replace the full `this.state.active` with the destructured value
  34. // If we need to refactor to swap some props for state, we only change it in one place
  35.  
  36. render() {
  37. const { profile } = this.props
  38. const { active } = this.state
  39.  
  40. return (
  41. <li>
  42. <div className="profile-card">
  43. <header className="profile-header" onClick={this.toggleClass}>
  44. <img src={profile.image} alt={profile.name} />
  45. <h2>{profile.name}</h2>
  46. </header>
  47. <section
  48. className={
  49. active ? "skills-container hidden" : "skills-container"
  50. }
  51. >
  52. <h4>Skills</h4>
  53. <ul className="skills-list">
  54. {profile.skills.map(skill => {
  55. return <li key={skill}>{skill}</li>
  56. })}
  57. </ul>
  58. </section>
  59. </div>
  60. </li>
  61. )
  62. }
Add Comment
Please, Sign In to add comment