Gorakh Nath
Gorakh Nath

Reputation: 9858

Not able to find real difference between .jsx and js file

My .js file is:-

var React = require('react');
export default class AmortizationChart extends React.Component {
    render() {
        var items = this.props.data.map(function (year, index) {

            return (

                <tr key={index}>

                    <td>{index + 1}</td>

                    <td className="currency interest">{Math.round(year.interestY).toLocaleString()}</td>

                    <td className="currency">{Math.round(year.balance).toLocaleString()}</td>

                </tr>

                );

});

Now same I can Write also in .jsx file.So what is the difference which file I sould take .js or .jsx?

Upvotes: 0

Views: 307

Answers (1)

James Donnelly
James Donnelly

Reputation: 128791

This bit is JSX:

<tr key={index}>
  <td>{index + 1}</td>
  <td className="currency interest">{Math.round(year.interestY).toLocaleString()}</td>
  <td className="currency">{Math.round(year.balance).toLocaleString()}</td>
</tr>

This only works because you're loading your .js file as text/babel. Try it with text/javascript and you'll get the following error:

Uncaught SyntaxError: Unexpected token <

React's own documentation has a good article on JSX here: JSX In Depth.

Upvotes: 3

Related Questions