Code Monkey home page Code Monkey logo

Comments (25)

Ehesp avatar Ehesp commented on July 20, 2024 2

You need to import it...

import React, { Component, PropTypes, View, Text, Image, IntentAndroid } from 'react-native';

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Hello

In the demo, the navigator is created by reference into the context of the app, meaning you can access it from any child component of the index.android.js file. In this case, the entire app.

https://github.com/react-native-material-design/demo-app/blob/master/src/scenes/Navigation.js#L81

The link above shows how you can grab it from context, and use it wherever you like.

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

How can I access them outside of the render() function?

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Same way you'd access props or state.

const { navigator } = this.context

navigator.pop();

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

Thanks for the swift response and help!

I tried too hard and couldn't figured it out, i'm sure i'm missing here something.
Here's my welcome screen code, i'm using react-native-gifted-listview module as my refreshable list component.

When i'm trying to console.log(navigator) i'm getting undefined.
Maybe you'll be able to help me out with this.

import React, { Component, View, Text, Image, IntentAndroid } from 'react-native';
import { Card, Button, COLOR, TYPO } from 'react-native-material-design';

import AppStore from '../stores/AppStore';

var API_URL = 'http://www.geektime.co.il/appApi/json.php';
var GiftedListView = require('react-native-gifted-listview');
var Post = require ('./Post');
var connError = 0;

export default class Welcome extends Component {
  _onFetch(page = 1, callback, options) {
    var data = [];
    function timeout(ms, promise) {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject(new Error("timeout"))
        }, ms)
        promise.then(resolve, reject)
      })
    }

    timeout(5000,fetch(API_URL + '?page=' + page)
      .then((response) => response.json())
      .then((responseData) => {
        if (connError == 1) {
          connError = 0;
        }
        data = responseData.posts;
        page++;
          callback(data);
      }).catch((error) => {
        if (connError == 0) {
            connError = 1;
        }
        callback(null);
      }).done()
    )
  }
    _renderCardView(rowData) {
      const navigator = this.context;
      const theme = AppStore.getState().theme;
      return (
        <View>
            <Card>
                <Card.Media
                    image={<Image source={{uri:rowData.image}}/>}
                    overlay
                >
                    <Text style={[TYPO.paperFontHeadline, COLOR.paperGrey50]}>{rowData.title}</Text>
                    <Text style={[TYPO.paperSubhead, COLOR.paperGrey50]}>{rowData.author}</Text>
                </Card.Media>
                <Card.Body>
                    <Text>{rowData.excerpt}</Text>
                </Card.Body>
                <Card.Actions position="left">
                    <Button primary={theme} value="Read this" onPress={() => console.log(navigator) } />
                </Card.Actions>
            </Card>
        </View>
      )
    }
    render() {
        return (
          <GiftedListView
            rowView={this._renderCardView}
            onFetch={this._onFetch}
            firstLoader={true} // display a loader for the first fetching
            pagination={true} // enable infinite scrolling using touch to load more
            refreshable={true} // enable pull-to-refresh for iOS and touch-to-refresh for Android
            withSections={false} // enable sections
            customStyles={{
              refreshableView: {
                backgroundColor: '#eee',
              },
            }}

            PullToRefreshViewAndroidProps={{
              colors: ['#ff0000', '#00ff00', '#0000ff'],
              progressBackgroundColor: '#c8c7cc',
            }}
          />
        );
    }

}

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

No problem!

this.context is an object, containing the navigator and drawer references. You need to do either:

const { navigator } = this.context; or
const navigator = this.context.navigator;

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

Tried them both, nothing works. headache.
Mind if I wrap the whole code in a zip and send it to you?

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Ah wait, you're missing something:

    static contextTypes = {
        navigator: PropTypes.object.isRequired
    };

That should work now, I hope ha.

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

Can't find variable: PropTypes

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

Works, but returns the WorkerNavigator instead of the navigator.

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Erm, WorkerNavigator? Can you dump the console.log of the navigator here?

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

screen shot 2016-01-04 at 3 02 52 pm

_renderCardView(rowData) {
      const theme = AppStore.getState().theme;
      console.log(navigator); // prints the following.
      return (
        <View>
            <Card>
                <Card.Media
                    image={<Image source={{uri:rowData.image}}/>}
                    overlay
                >
                    <Text style={[TYPO.paperFontHeadline, COLOR.paperGrey50]}>{rowData.title}</Text>
                    <Text style={[TYPO.paperSubhead, COLOR.paperGrey50]}>{rowData.author}</Text>
                </Card.Media>
                <Card.Body>
                    <Text>{rowData.excerpt}</Text>
                </Card.Body>
                <Card.Actions position="left">
                    <Button primary={theme} value="Read" />
                </Card.Actions>
            </Card>
        </View>
      )
    }

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Okay so I went about and tackled this...

https://github.com/react-native-material-design/demo-app#navigate

Custom implementation which is pretty flexible. Might make it into a lib but could do with testing on it first :)

from react-native-material-design.

arpu avatar arpu commented on July 20, 2024

with more cards i get Cannot read property '_currentElement' of null

when i use const { navigator } = this.context;

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Could you show me the full code?

from react-native-material-design.

arpu avatar arpu commented on July 20, 2024

http://lpaste.net/148531

from react-native-material-design.

Salakar avatar Salakar commented on July 20, 2024

@arpu can you also show us your routes file please

from react-native-material-design.

arpu avatar arpu commented on July 20, 2024

is the routes file from the demo-app nothing changed

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Could you try replacing the Button with this:

<Button primary={theme} value="Read this" onPress={() => { navigator.forward() }} />

If that doesn't work, can you paste the output of this.context.navigator for us... cheers

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

TypeError: Cannot read property 'navigator' of undefined(…)
ExceptionsManager.js:63 Cannot read property '_currentElement' of nullhandleException @ ExceptionsManager.js:63handleError @ InitializeJavaScriptAppEngine.js:80ErrorUtils.reportFatalError @ error-guard.js:28guard @ MessageQueue.js:40callFunctionReturnFlushedQueue @ MessageQueue.js:81onmessage @ debuggerWorker.js:39

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Try changing:
_renderCardView(rowData) {

to:
_renderCardView = (rowData) => {

from react-native-material-design.

avishayil avatar avishayil commented on July 20, 2024

This solves the issue.

from react-native-material-design.

Ehesp avatar Ehesp commented on July 20, 2024

Okay so this is because within _renderCardView(rowData) {, the class (this) isn't bound into that function. To get around this you could do something in the constructor like:

constructor() {
    super();
    this._renderCardView = this._renderCardView.bind(this);
}

However, that to me is a bit stinky, so doing:

_renderCardView = (rowData) => { is a cleaner way to bind this, using the arrow function:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

from react-native-material-design.

arpu avatar arpu commented on July 20, 2024

works for me too thx

from react-native-material-design.

Salakar avatar Salakar commented on July 20, 2024

Closing issue as this is resolved.

from react-native-material-design.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.