-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[CS2] JSX documentation, Try CoffeeScript improvements #4583
Conversation
…nk’ button, automatically save code in localStorage
documentation/sections/jsx.md
Outdated
|
||
[JSX](https://facebook.github.io/react/docs/introducing-jsx.html) is JavaScript containing interspersed XML elements. While conceived for [React](https://facebook.github.io/react/), it is not specific to any particular library or framework. | ||
|
||
CoffeeScript supports interspersed XML elements, without the need for separate plugins or special settings. The XML elements will be rendered as such, outputting JSX that could be parsed like any normal JSX file, for example by [Babel with the React JSX transform](https://babeljs.io/docs/plugins/transform-react-jsx/). CoffeeScript does _not_ output `React.createElement` calls or any code specific to React or any other framework. It is up to you to attach another step in your build chain to convert this JSX to whatever function calls you wish the XML elements to compile to. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The XML elements will be rendered compiled
Otherwise lgtm.
<Star className="halfStar" />} | ||
{for emptyStar in [Math.ceil(rating)...maxStars] | ||
<Star className="emptyStar" key={emptyStar} />} | ||
</aside> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the second line wraps, which I don’t think is such a big deal.
Just going to merge this in, if anyone else has any more notes please feel free to comment and I’ll make the changes as part of the next release docs. |
Finally got around to trying React, and noticed one improvement of the Coffeescript code over ES6 - namely, the ability to bind instance methods to the instance when making a For example, in the react docs on event handling, they have this example: class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
}
} the line With Coffeescript this could be written as: class Toggle extends React.Component
constructor: (props) ->
super(props)
@state = isToggleOn: true
handleClick: => # get to use arrow function here
@setState (prevState) => isToggleOn: !prevState.isToggleOn
render: -> (
<button onClick={@handleClick}>
{@state.isToggleOn ? 'ON' : 'OFF'}
</button>
) So, it's nice that instance methods can be easily bound in Coffeescript using arrow functions. This is just an idea for another example. I was reading through the longer discussions on CSX and remember there was interest in getting ideas for examples that show coffeescript's strengths. |
That’s a great point. Is there a place to include that in the docs, without the docs getting too long? |
maybe there could be a separate section about react in coffeescript, which shows a slightly larger code snippet, for a todo app or something. I got this simple counter-incrementor working in React Native, for example. Granted, it's pretty straightforward and not really doing anything really fancy with Coffeescript other than using its basic syntax. CounterButton = ({text, on_press}) ->
<Button title='counter' onPress={on_press}>{text}</Button>
CounterValue = ({value}) ->
<Text>{value}</Text>
class Counter extends React.Component
constructor: (props) ->
super(props)
@state = counter_val: 1
increment_counter: =>
@setState (old_state) ->
counter_val: old_state.counter_val + 1
render: ->
<View>
<CounterButton
on_press={@increment_counter}
text='increment counter'
/>
<CounterValue value={@state.counter_val} />
</View> What took me a little while to realize, though, is the fact that Coffeescript's JSX supports a wider range of things inside |
I was hoping to link to a few repos with example projects. Could you maybe post this as its own project with a README and I could link to it? It would be nice to have something that people could clone and get running in just a few steps. |
Adds a “JSX” section to the docs, and a note about the
<
and>
operators to the breaking changes. Follows #4551. @xixixao, would appreciate your review.https://rawgit.com/GeoffreyBooth/coffeescript/2-docs/docs/v2/#jsx
I also added some features to Try CoffeeScript to make it more similar to the Babel REPL. Now
location.hash
is set automatically whenever the code recompiles, and the Link button has been removed. We also save the user’s code automatically intolocalStorage
, so it’s there the next time they return.I also fixed (I hope) the code editors’ handling of tab-indented code, to avoid “inconsistent indentation” errors. The editors will always indent with two spaces by default; only if you paste in tab-indented code will they behave in tab-indented mode. Once at least one line in the code block begins with a tab, the tab key indents with tabs and the enter key causes new lines to be tab-indented. @lydell, @jashkenas, any notes?