Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove attribute merging for functional components with no prop definition #7557

Closed
blake-newman opened this issue Jan 29, 2018 · 0 comments
Closed

Comments

@blake-newman
Copy link
Member

blake-newman commented Jan 29, 2018

What problem does this feature solve?

Currently when using functional components without prop definition, all attributes will be rendered when using JSX spread.

This is because the context.data.attrs remain after they are merged.

Current

Usage:<Example type="blue" fill={true} class="extra-class" aria-label="I'm a label" />

Implementation:

export default {
  functional: true,
  
  render({ data, props }) {
    const { type, fill } = props

    const buttonClass = {
      'button--fill': fill,
      [`button--${type}`]: !!type
    }

    <button {...data} class={buttonClass} >
  }
}

Output: <button fill="true" type="blue" class="extra-class button--fill button--blue" aria-label="I'm a label" />

What does the proposed API look like?

It should be explicit what attributes to render to dom when using a functional component with no prop definition.

With fix delete data.attrs

Explicitly passing attributes

You can use spread notation to get all remaining attrs, and replacing the data.attrs object

Usage: <Example type="blue" fill={true} class="extra-class" aria-label="I'm a label" />

Implementation:

export default {
  functional: true,
  
  render({ data, props }) {
    const { fill, type, ...attrs } = props
    data.attrs = attrs

    const buttonClass = {
      'button--fill': fill,
      [`button--${type}`]: !!type
    }

    <button {...data} class={buttonClass} >
  }
}

Output: <button class="extra-class button--fill button--blue" aria-label="I'm a label" />

Explicitly passing attributes 2

Usage: <Example type="blue" fill={true} class="extra-class" formtarget="id" dummy-attr="lol" />

Implementation:

export default {
  functional: true,
  
  render({ data, props }) {
    const { fill, type, formtarget } = props

    const buttonClass = {
      'button--fill': fill,
      [`button--${type}`]: !!type
    }

    <button {...data} formtarget={formtarget} class={buttonClass} >
  }
}

Output: <button class="extra-class button--fill button--blue" formtarget="id" />

Conclusion

Both examples show how the behaviour of the attributes should be left with the child when dictating what attributes shoud be rendered. You can either do a and rest approach or explcititly name attributes to pass onto element.

The original all props/attrs will be rendered as attributes, which adds polution to the DOM especially with attributes that are array's/objects/functions/symbols.

@blake-newman blake-newman changed the title Remove attribue merging for functioal components with no prop definition Remove attribute merging for functional components with no prop definition Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant