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

Can coffee-react be used directly in a Browser? #23

Closed
hanzhao opened this issue May 4, 2015 · 2 comments
Closed

Can coffee-react be used directly in a Browser? #23

hanzhao opened this issue May 4, 2015 · 2 comments

Comments

@hanzhao
Copy link

hanzhao commented May 4, 2015

As coffee-script, browsers use /extras/coffee-script.js with tag <script src="text/coffeescript> to write or reference to coffeescript directly in a browser. Can coffee-react do the same currently?

@jsdf
Copy link
Owner

jsdf commented May 7, 2015

Sure, I've just published a new version which adds a 'browser' script, which you can download from browserifyCDN: https://wzrd.in/standalone/coffee-react-browser (or you can make a browserify bundle of coffee-react/lib/browser.js yourself if you prefer).

example

<!DOCTYPE html>
<html>
<head>
  <title>CJSX in a script tag</title>
  <script src="https://fb.me/react-0.13.2.js"></script>
  <script type="text/javascript" src="https://wzrd.in/standalone/coffee-react-browser"></script>
  <script type="text/cjsx">
    # defining a component
    class Clock extends React.Component
      @defaultProps = interval: 2000

      constructor: (props) ->
        super props
        @state = time: new Date

      componentDidMount: ->
        setInterval(@tick, @props.interval)

      tick: =>
        @setState time: new Date

      render: ->
        <h1>{@state.time.toLocaleTimeString()}</h1>

    # rendering to the page
    React.render(<Clock interval={100} />, document.body)
  </script>
</head>
<body>

</body>
</html>

@hanzhao
Copy link
Author

hanzhao commented May 7, 2015

Nice! Thanks a lot!

@jsdf jsdf closed this as completed May 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants