react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Bundles

Open 7ynk3r opened this issue 11 years ago • 13 comments

I'm in love with react native. Programming for iOS is much much much simpler than ever!

I've been playing with the sample app and everything went right but now I'm trying to do something more realistic and I'm having some issues.

I'm trying to recreate an app similar to Movies. The problem I have is that I'm not able to include other js files. I can load a single js file but any require inside it fails (but I can load it if I set it as the root component).

I'm using this url in the AppDelegate.m

http://localhost:8081/FlightDealsApp.includeRequire.runModule.bundle

When I try to include a require like this

var FlightList = require('./FlightList');

I got this error

Error: Expected relative module lookup from /Users/.../dev/mobile/FlightDeals/ReactComponent/FlightDealsApp.js to ./FlightList to be within a package but no package.json found.
[...]

Thanks! JM

7ynk3r avatar Apr 20 '15 02:04 7ynk3r

Do you have module.exports in FlightList.js?

eduardinni avatar Apr 22 '15 05:04 eduardinni

Hello @eduardinni!

A couple of details I forgot to mention:

  • I'm trying to use the approach described in http://facebook.github.io/react-native/docs/embedded-app.html#content
  • I'm using React Native (v~0.2) through cocoa pods
pod 'React'
pod 'React/RCTText'
pod 'React/RCTImage'
pod 'React/RCTNetwork'
  • I'm able to request both files through http:
    • http://localhost:8081/FlightDealsApp.includeRequire.runModule.bundle (without requiring FlightList)
    • http://localhost:8081/FlightList.bundle
  • This is the content of both js files and the app delegate

FlightDealsApp.js

'use strict';
var React = require('react-native');
var { AppRegistry, Text, View } = React;
// var FlightList = require('./FlightList'); // Uncommenting this will make the app to fail.
var FlightDealsApp = React.createClass({
  render: function() { 
        return (
        <View style={{backgroundColor: '#527FE4', padding: 10, margin: 10}}>
          <Text style={{fontSize: 11}}>Hello</Text>
        </View>
      );}
});
AppRegistry.registerComponent('FlightDealsApp', () => FlightDealsApp);
module.exports = FlightDealsApp;

FlightList.js

'use strict';
var React = require('react-native');
var { AppRegistry, Text, View } = React;
var FlightList = React.createClass({
  render: function() { 
        return (
        <View style={{backgroundColor: '#527FE4', padding: 10, margin: 10}}>
          <Text style={{fontSize: 11}}>World</Text>
        </View>
      );}
});
module.exports = FlightList;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/FlightDealsApp.includeRequire.runModule.bundle"];

    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"FlightDealsApp"
                                                     launchOptions:launchOptions];

    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *rootViewController = [[UIViewController alloc] init];
    rootViewController.view = rootView;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

Is this the correct approach for new apps?

Thanks for responding! I really love this technology, I think is the future for all FE apps!

7ynk3r avatar Apr 22 '15 18:04 7ynk3r

Just to let everybody know, the pod version doesn't support "require". I think it's because it's a little outdated.

Use react-native init [MY_PROJECT_NAME] to create a new project from scratch. Then use pods if you need other dependencies.

http://facebook.github.io/react-native/docs/tutorial.html#content

7ynk3r avatar Apr 28 '15 01:04 7ynk3r

I believe you can also point CocoaPods at node_modules/react-native. I'm doing something like that.

ide avatar Apr 28 '15 01:04 ide

@ide can you share your pod file? I hit the same issue.

yosit avatar May 06 '15 06:05 yosit

@yosit:

pod 'React', :path => '../react-native', :subspecs => [
  'Core',
  'RCTImage',
  'RCTNetwork',
  'RCTText',
  'RCTWebSocket',  # used to be RCTWebSocketDebugger prior to 0.4.4
]

Be sure to change the path depending on your setup.

ide avatar May 06 '15 07:05 ide

This is amazing!

It's almost working for me but for some reason Text components are not being render even tough I can see the background color changing.

The server responds with the appropriated bundle, but the application is not able to render the content properly.

Ideas?

What I did:

  1. cloned react-native under ./react-native (0.4.4)
  2. created a new project under ./my_project (xCode 6.3.1).
  3. created the podfile with @ide content and run pod install
  4. created an index.ios.js file
'use strict';
var React = require('react-native');
var {  AppRegistry,  StyleSheet,  View,  Text } = React;

class index extends React.Component{
  render() {
    return (
      <View style={styles.container}>
      <Text>This is a simple application.</Text>
      </View>
    )
  }
};

var styles = React.StyleSheet.create({
  container: { flex: 1, backgroundColor: 'green' }
});

AppRegistry.registerComponent('index', () => index);
module.exports = index;
  1. setup the appDelegate.m to work with index.ios.js
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSURL *jsCodeLocation;
    jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle"];
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"index"
                                                     launchOptions:launchOptions];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *rootViewController = [[UIViewController alloc] init];
    rootViewController.view = rootView;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    return YES;
}
  1. create a package.json file under ./my_project.
{
  "name": "my_project",
  "version": "0.0.1"
}
  1. started the dev server
(JS_DIR=`pwd`; cd ../react-native/React; npm run start -- --root $JS_DIR)
  1. runned the app in xcode.

7ynk3r avatar May 19 '15 16:05 7ynk3r

I've solved the problem, it was cocoapods that was outdated. It was ignoring the subspecs and just using the default Core instead. There was no error during the process.

After that I was able to install pods without almost any problem.

The only minor thing was that RCTWebSocketDebugger doesn't exists. I think it was renamed to RCTWebSocket, can you confirm this @ide?

It would be nice to add this to the docs. If I can help with that I'll happy to.

Cheers, JM

7ynk3r avatar May 20 '15 05:05 7ynk3r

@7ynk3r, that's correct. One of the recent updates to react renamed the pod subspec to RCTWebSocket.

ide avatar May 20 '15 06:05 ide

I hit the same problem. It looks like react native pod version doesn't support "require". both @ide and @7ynk3r ways is not working for me. While If I create project with react-native init. It works.

studentdeng avatar May 21 '15 06:05 studentdeng

You are right @studentdeng . You need to add a package.jsonfile to the root ./my_project.

{
  "name": "my_project",
  "version": "0.0.1"
}

I think this is because this is a project that is based on node and react-native is expecting this to be there for some reason but it shouldn't be necessary given that when you start the server your are specifying the path.

7ynk3r avatar May 22 '15 05:05 7ynk3r

I bumped up against this problem as well when following the instructions at https://facebook.github.io/react-native/docs/embedded-app.html#content.

The problem was with the folder structure. At the end of the tutorial I ended up with the following file hierarchy

.
..
ReactComponent
ReactComponent/index.ios.js
ReactComponent/anotherClass.js
package.json

which produced the errors seen above. It seems that packager expects a package.json file in the directory where index.ios.js is located?

I solved the problem by symlinking to package.json. So my final directory structure is

.
..
ReactComponent
ReactComponent/index.ios.js
ReactComponent/anotherClass.js
ReactComponent/package.json -> ../package.json
package.json

Also, in case it helps, this is what my package.json file looks like

{
  "name": "TestApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "./Pods/React/packager/packager.sh --root ./ReactComponent/"
  },
  "dependencies": {
    "react-native": "^0.5.0",
    "react-native-carousel": "^0.3.3",
    "react-native-device": "^0.2.0",
    "react-native-i18n": "0.0.4",
    "react-native-side-menu": "^0.8.3",
    "react-tween-state": "^0.1.1",
    "rebound": "0.0.13",
  }
}

sghiassy avatar Jul 03 '15 17:07 sghiassy

Nice one @sghiassy - thanks for sharing!

brentvatne avatar Jul 07 '15 20:07 brentvatne