Using Composition to handle UX variations

Combining smaller reusable components to build a bigger UI blocks.

How do we make sure components are reusable?

  • By ensuring our UI components are pure presentational components (dumb)

What does reusable mean?

  • No data fetching within the component (do it in Redux).
  • If data is required from API - goes into Redux
    Via redux-thunk API calls are isolated away from the redux containers that deal with the data obtained and pass it on to the dumb component.

If we have a bunch of renderBla() functions within the component which are used in the main component render()

  • It’s better to move it to separate components. That way it is reusable.

Example

Login page variations

UX variations toggle features + add in additional links/markup.

If the UX variations are involved toggling features within a component + adding minor markup around it

  1. import React, { Component } from "react";
  2. import PropTypes from 'prop-types';
  3. import SignIn from "./sign-in";
  4. class MemberSignIn extends Component {
  5. _renderMemberJoinLinks() {
  6. return (
  7. <div className="member-signup-links">
  8. ...
  9. </div>
  10. );
  11. }
  12. _routeTo() {
  13. // Routing logic here
  14. }
  15. render() {
  16. const {forgotEmailRoute,forgotPwdRoute, showMemberSignupLinks} = this.props;
  17. return (
  18. <div>
  19. <SignIn
  20. onForgotPasswordRequested={this._routeTo(forgotPwdRoute)}
  21. onForgotEmailRequested={this._routeTo(forgotEmailRoute)}>
  22. {this.props.children}
  23. {showMemberSignupLinks && this._renderMemberJoinLinks()}
  24. </SignIn>
  25. </div>
  26. );
  27. }
  28. }
  29. export default MemberSignIn;