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.

An example of integrating with React without the Foundation / Adapter pattern

See original GitHub issue

Notes

This is not a feature request or a bug, so if there is a better way to communicate this, please let me know and I will close this PR.

My main purpose in opening up this issue is to get feedback on this approach for integrating material-components-web with React. Does this simple approach miss anything important?

Integration approach

I am attempting to use material-components-web with React for a production web app. I found a way of integrating with React that is much more lightweight than the Foundation / Adapter pattern.

The basic idea is create and destroy vanilla material-component-web JS components based on the React lifecycle. I hook up the material component to the right element of the DOM by using the react API to get a refence to a DOM element.

Example

import React from 'react'
import { textfield } from 'material-components-web'

export default class TextField extends React.PureComponent {
  
  // Instantiate the vanilla material component once the react component mounts
  componentDidMount() {
    this.mdcComponent = new textfield.MDCTextfield(this.mdcMount)
  }

  // Clean up the vanilla material component along with the react component 
  componentWillUnmount() {
    this.mcdComponent.destroy()
  }

  render() {
    // Note in the second opening div how I get a reference to the DOM element of the text field
    return (
      <div className="mdc-form-field">
        <div className="mdc-textfield" ref={(div) => { this.mdcMount = div }}>
          <input
            type="text"
            className="mdc-textfield__input"
            id="demo-input"
            onChange={this.props.onChange}
          />
          <label htmlFor="demo-input" className="mdc-textfield__label">
            {this.props.label}
          </label>
        </div>
      </div>
    )
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
traviskaufmancommented, Apr 27, 2017

Since there’s been no discussion here for a month, closing. That being said, we have begun informal, not-at-all planned out talks about how we can simplify our adapter pattern approach in future releases of the library.

3reactions
amsheehancommented, Mar 21, 2017

To follow up from before after discussing a little more with the rest of the core team:

There’s nothing inherently wrong with this approach on the web. It may get you varying milage component to component. However, some people may want to take an isomorphic approach to their React app, some people may be writing a React Native app, some people may be using a state container.

For these instances, where you need a Material UI library that works across environments, you would need to use the foundation/adapter pattern. This allows us to use components that have different considerations when identifying elements and all that’s associated with them in a regular browser environment (capturing/handling events, observing changes, etc…).

We would love to hear any and all ideas for how to make these abstractions easier on frameworks. Please continue the discussion here, update us on the progress of the apps you’re building and let us know if there’s anything we can do to help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use the Adapter Design Pattern in React
The adapter pattern converts the interface of a class into another interface ... For our example, we'll create an adapter for React Flow, ......
Read more >
Adapter Method - Python Design Patterns - GeeksforGeeks
Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method ...
Read more >
Implementing a Consumer-Driven Contract for a React App ...
A tutorial that shows how to implement a REST consumer with Axios, build a a consumer-driven contract for it with the Pact framework, ......
Read more >
Using the Adapter Design Pattern With React - SendGrid
Adapter pattern to the rescue​​ An adapter is essentially just a translation layer that connects two different interfaces together. The ...
Read more >
MDC-112 Web: Integrating MDC with Web Frameworks
The Adapter is an interface. The Adapter interface is referenced by the Foundation to implement Material Design business logic. You can ...
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