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

Support @bound decorator #3635

Closed
haraldrudell opened this issue Dec 21, 2017 · 6 comments
Closed

Support @bound decorator #3635

haraldrudell opened this issue Dec 21, 2017 · 6 comments

Comments

@haraldrudell
Copy link

haraldrudell commented Dec 21, 2017

Use of the @bound decorator is important to React people

Instead of having the hack:

…class ... {
  clickHandler = e => e.preventDefault() + doRealWork()
  
}

We would want to write, in the name of canonical stack traces:

.class ... {
  @bound clickHandler(e) {
    e.preventDefault()
    doRealWork()
  }

This is made possible by decorators from the fine @babel project
And we can use @brigand class-bind or other to get this done today

I apologize for this looking like 90s Java code, but it is actually great

Here’s the preparation:

npm install --save class-bind
// if the world had BabelJS 7
//npm install --save-dev babel-plugin-transform-decorators
npm install babel-plugin-transform-decorators-legacy --save-dev
// teach babel-preset-react-app to load the babel plugin transform-decorators-legacy

And here’s a test-patch for src/App.js:


import {bound} from 'class-bind'

class App extends Component {
  constructor(...args) {
    super(...args)
    const f = this.boundTest
    f(this)
}

@bound boundTest(properThis) {
  console.log(this === properThis
    ? '@bound annotation working correctly'
    : 'function was not bound')
  }

The browser console output:

@bound annotation working correctly

Here’s what it could look like when live:

export default class Parent extends Component {
  state = {one: 'One', two: 'Two'}

  @bound submit(e) {
    e.preventDefault()
    const values = {...this.state}
    console.log(`${m}.submit:`, values)
  }

  @bound fieldUpdate({name, value}) {
    this.setState({[name]: value})
  }

  render() {
    console.log(`${m}.render`)
    const {state, fieldUpdate, submit} = this
    const p = {fieldUpdate}
    return (
      <form onSubmit={submit}> {/* loop removed for clarity */}
        <Child name='one' value={state.one} {...p} />
        <Child name='two' value={state.two} {...p} />
        <input type="submit" />
      </form>
    )
  }
}

Awesomeness and profit…

@102
Copy link

102 commented Dec 26, 2017

#411 (comment)

@gaearon
Copy link
Contributor

gaearon commented Jan 9, 2018

Yeah, we don't plan to support decorators until their specification is more stable. There is no Babel plugin that implements the current specification so supporting them now would create a burden on our users in the future.

@gaearon gaearon closed this as completed Jan 9, 2018
@corysimmons
Copy link

@gaearon Is there a particular stage you guys are waiting for? Not critiquing, just curious.

Dropping this here to watch for everyone interested in decorator support babel/proposals#11

@miraage
Copy link

miraage commented Oct 8, 2018

It's really interesting. @gaearon what proposal stage is acceptable for a certain feature to be included in CRA?

@Timer
Copy link
Contributor

Timer commented Oct 8, 2018

@miraage we generally only accept stage 4 (finalized) or very few stage 3 if they're known to be exceptionally stable, or we're willing to provide a code mod migrating people off the specification

@miraage
Copy link

miraage commented Oct 8, 2018

@Timer thank you for the information

@lock lock bot locked and limited conversation to collaborators Jan 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants