0

I am completely new to React Native. So please apologize me for very naive questions.

I have a main screen which has a button. When the button is pressed I want to fetch data thru another component and display on the screen.

I have App.js

import React from 'react';
import { Button, View, Text } from 'react-native';
import { createAppContainer, createStackNavigator } from 'react-navigation'; // Version can be specified in package.json

import {getMovies} from "./fetchmenu.js";

class HomeScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
          <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
            <Button
              title="Menu"
                onPress={() => {
                    <getMovies />   // I want to display data as a result of button press
                  }}
            />
          </View>
      </View>
    );
  }
}

const AppContainer = createAppContainer(RootStack);

export default class App extends React.Component {
  render() {
    return <AppContainer />;
  }
}

And fetchmenu.js

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class getMovies extends React.Component {

  constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
    return fetch('https://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: responseJson.movies,
        }, function(){

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }

  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
          <View style={{flex: 1, paddingTop:20}}>
            <FlatList
              data={this.state.dataSource}
              renderItem={({item}) => <Text>{item.title}, {item.releaseYear}</Text>}
              keyExtractor={({id}, index) => id}
            />
          </View>
    );
  }
}

How do I display the FlatList created by getMovies on HomeScreen when the button is pressed?

2 Answers 2

2

In your HomeScreen set your state like this:

state={showMovies:false}

Then on Button click setState to {showMovies:true}. And in your render function in App.js:

render() {
return (
  <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Home Screen</Text>
      <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
        <Button
          title="Menu"
            onPress={() => {
                this.setState({showMovies:true});
              }}
        />
        {this.state.showMovies&&<GetMovies/>}
      </View>
  </View>
);
}

Since you are exporting getMovies class as default export (not named) do the same with importing:

WRONG: import {getMovies} from "./fetchmenu.js";

CORRECT: import GetMovies from "./fetchmenu.js";

I strongly recommend you to watch some tutorials before getting into real projects.

EDIT

As @Milore stated Component names in React must start with capital letter. See this for further information

Sign up to request clarification or add additional context in comments.

3 Comments

As you noted, components MUST have a capital first letter name
Thanks. It was helpful. Can you please update your response with two things to make it more useful for other newbies like me (a) state={showMovies:false} should be inside HomeScreen; and (b) {this.state.showMovies&&<GetMovies/>} should have a closing curly bracket as well :)
@AbdumutalAbdusamatov thanks for your help. I have posted a follow-up question to get down to practical level. Here is the link: stackoverflow.com/questions/54909733/…
0
constructor(props){
    super(props);
    this.state ={ isLoading: true}
}

I don't see dataSource in this.state but you are using

this.setState({
   isLoading: false,
   dataSource: responseJson.movies,
   }, function(){
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.