question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: react component HOCs allowing for bringing your own component

See original GitHub issue

Do you want to request a feature or report a bug?

feature

current behavior

The current set of react components bring their own, strongly opinionated DOM.

Example:

<Authenticator />

brings it’s own specific versions of <AmplifyUI.InputRow /> , <AmplifyUI.FormRow />, etc, and each of those brings an explicit <html> element of its own choosing.

This is problematic because it hinders extension. For instance, what if you need to use your own component library’s visual components, whilst still getting the benefit of the amplify HOC? Consider if your component library has deep prebaked CSS styles & JS bindings on something as simple as an <Input />. you may care to use said <Input />, but instead must use the <input /> that <AmplifyUI.InputRow />owns completely, and style it through the theme interface. The theme interface is a great start, but adds complication when really all you want out of the library is the logic and perhaps component structure, not the aesthetics or DOM.

expected behavior

luckily, this is a solved problem in other react spaces. there are two implementations that i use commonly, and thus cite both for reference

  • react-styleguidist
    • essentially, they let you create a map between their internal HOC’s and your own components. your own components just need to render children and pass thru key top-level props

here’s a hypothetical usage:

// some amplify aware file
const theme = {
  ...,
  components: {
     FormRow: MyCustomFormRow,
     InputRow: MyCustomInputRow
   }
}

// AmplifyUI.jsx
...
export const FormRow = (props) => {
    const theme = props.theme || AmplifyTheme;
    const style = propStyle(props, theme.formRow);
    const p = JS.objectLessAttributes(props, 'theme');
    const ElementType = getElementType('FormRow', theme) // returns MyCustomFormRow if provided, or, the previous default, `'div'`
    return beforeAfter(
        <ElementType {...p} className={themedClassName("amplify-form-row", theme.formRow)} style={style}>
            {props.children}
        </ElementType>
    )
}
  • semantic-ui-react, aka SUIR
    • SUIR lets you use any of their components with an as prop.
    • e.g. <Header as={SomeOtherComponent />, vs. the default <h1 />, etc

The SUIR strategy works best when composing components from the bottom up. The styleguidist approach works best when users want to use a predefined component structure, but with tweaks to the DOM tree. Most likely, a styleguidst approach would be the ideal 1st pass, with consideration for using the SUIR approach longer term.

I’d be happy to PR this if accepted.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nidsharmcommented, May 31, 2018

Hi @cdaringe,

Thank you for the suggestion. We do accept PRs, and I have added this to our backlog too.

0reactions
github-actions[bot]commented, Oct 17, 2021

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >
React - Introducing Server and Shared Components
Explaining the idea behind the new experimental types of React components - Server Components and Shared Components, allowing to have rich ...
Read more >
Higher-Order Components In React - Smashing Magazine
A higher-order component (HOC) is an advanced element for reusing logic in React components. Components take one or more components as arguments ...
Read more >
Vue Composition API vs React Hooks Quick Comparison
Vue recently released the Vue Composition API RFC which is an additive API that changes the setup of Vue components to make use...
Read more >
React Higher-Order Components in TypeScript - Medium
Higher-order components (HOCs) in React are a powerful tool for code reuse between components. However, a common complaint of developers using TypeScript is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found