Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make components in React Native without using JSX?

The canonical way of using React Native is apparently with JSX:

render: function() {
  var movie = MOCKED_MOVIES_DATA[0];
  return (
    <View style={styles.container}>
      <Text>{movie.title}</Text>
      <Text>{movie.year}</Text>
      <Image source={{uri: movie.posters.thumbnail}} />
    </View>
  );
}

How would I do this using JavaScript only? Typically in normal React, React.createElement('div') would work as an alternative to JSX, but I get an error "null is not a function" when trying to run my iOS React Native app.

like image 672
André Staltz Avatar asked Mar 26 '15 19:03

André Staltz


People also ask

Can we write React component without JSX?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.

What is alternative to JSX in React?

React, ES6, JavaScript, EJS, and Python are the most popular alternatives and competitors to JSX.

Can I use JS instead of JSX?

JS is simply a scripting language, adding functionality into your website. JSX is an addition to the JavaScript syntax which is a mixture of both HTML and JavaScript. Both JS and JSX are interchangeable but JSX makes the code easier to understand for users.


1 Answers

I tried contacting the packager, which says it listens on port 8081 and also says that it is getting requests for index.ios.bundle as it runs.

So I put this in my browser: http://localhost:8081/index.ios.bundle

In the returned bundle, I found:

var wazoo = React.createClass({displayName: "wazoo",
  render: function() {
    return (
        React.createElement(View, {style: styles.container}, 
          React.createElement(ScrollView, null, 
            React.createElement(View, null, 
                React.createElement(Text, {style: styles.welcome}, 
                  "Wazoo"
                ), 

And so on. So it looks like View, ScrollView etc. are just like components as defined as usual in Web React, and the above code is the JS equivalent to the JSX.

like image 83
Daniel Earwicker Avatar answered Oct 20 '22 04:10

Daniel Earwicker