nikgraf / belle

Configurable React Components with great UX
http://nikgraf.github.io/belle/
MIT License
2.51k stars 104 forks source link

uniqueId() generation breaks server side rendering #326

Closed felix-d closed 3 years ago

felix-d commented 7 years ago

For example, in TextField

https://github.com/nikgraf/belle/blob/master/src/components/TextInput.js#L139 This breaks server side rendering because a new id is generated on the client.

warning.js:45Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server: (client) to;" class="style-idb6de" id="username" (server) to;" class="style-idd07b" id="username"

I guess you could use key or id from props to create that id and fallback on a random id.

export default class TextInput extends Component {

  constructor(properties) {
    super(properties);
    ...
    this.uniqueId = properties.key;
  }

  /**
   * Generates the style-id & inject the focus & hover style.
   */
  componentWillMount() {
    const id = this.uniqueId || uniqueId();
    this._styleId = `style-id${id}`;
    updatePseudoClassStyle(this._styleId, this.props);
  }
mcbryant commented 7 years ago

I'm interested in fixing this!

Regarding your suggestions, @felix-d:

Here's two similar ways of solving this:

I'd be interested in implementing the style-hash ID approach. Am I missing another way? Thoughts, @felix-d and @nikgraf?

felix-d commented 7 years ago

@mcbryant

An "id" prop would absolutely solve this issue! It would be a bit painful to require an "id" (or similar) prop for every component when you want server side rendering, though.

This is what Material UI does AFAIK.

The hashing solution seems like a good idea but it's more complex. I'd go with the id solution first to fix the issue, and then maybe think about an alternative solution that involves hashing, but the id solution is quite viable and solid šŸ‘ Simple is always better than complex šŸ˜„

Good reasoning!