React native button click render different components

krisna

I am using Expo React native for my app. I have created three buttons. When user will click the button it will fetch data and render it in one screen. Each button will button will fetch different api. Under the buttons there will be flatlist, where it will display the data. I can do with one button click display the data but I could not figure how to display other buttons api. I share my code in codesandbox. ps: Below code make this app get super slow

This is my code

import React from 'react';
import { Text, View, ScrollView } from 'react-native';
import styled from 'styled-components';
import PressableButton from './Button';
import axios from 'axios';
const api = "https://jsonplaceholder.typicode.com/users";
const anApi = "https://jsonplaceholder.typicode.com/photos"

const Data = ({ data }) => {

  return (
    <View style={{ flex: 1 }}>
      <Text>{JSON.stringify(data, null, 4)}</Text>
    </View>
  )
}
const AnData = ({ andata }) => {
  return (
    <View style={{ flex: 1 }}>
      <Text>{JSON.stringify(andata, null, 1)}</Text>
    </View>
  )
}

export default function App() {
  const [data, setData] = React.useState([]);
  const [anotherdata, setanotherData] = React.useState([]);


  const updateState = async () => {
    await axios(api)
      .then((res) => {
        setData(res.data);
      })
      .catch((err) => {
        console.log("failed to catch", err);
      });
  };

  const anoThereState = async () => {
    await axios(anApi)
      .then((res) => {
        setanotherData(res);
      })
      .catch((err) => {
        console.log("failed to catch", err);
      });
  };

  return (
    <React.Fragment>
      <Container>
        <PressableButton onPress={updateState} title='First button' bgColor='#4267B2' />
        <PressableButton onPress={anoThereState} title='Second button' bgColor='lightblue' />
        <PressableButton onPress={() => true} title='Third button' bgColor='#4267B2' />
      </Container>
      <Scroll>
        {data && data === undefined ? <Text>loading</Text> : <Data data={data} />}
        {anotherdata && anotherdata === undefined ? <Text>loading</Text> : <AnData andata={anotherdata} />}
      </Scroll>
    </React.Fragment>
  );
}

const Container = styled.View`

 flex-direction: row;
 justify-content: center;
 padding: 70px 0px 20px 0px;
`;

const Scroll = styled.ScrollView`
flex: 1;
`
Ashutosh Dubey

Under the buttons there will be flatlist

You're not using FlatList, only showing response in text and the data for second button is huge that's why It's super slow.

Here are the changes I made, check if this is what you're looking for?

Also if you want one data to show at a time you can either use tabs or show/hide the data depending on selection like I've done in code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React-native, render a button click dynamically

From Dev

React-native, render a button click dynamically

From Dev

Is there a better or more elegant way to conditionally render different components in React Native?

From Java

Not sure how to render this component with click of a button in react-native

From Dev

How to render components using function in React Native

From Dev

react.js render components at different locations

From Dev

Duplicate components in React after click button

From Dev

How to navigate to different screen on button click in react-native android app using react-navigation

From Dev

Render a component onPress(TouchableOpacity/Button) in React Native

From Dev

Signleton between different components in React Native

From Dev

React native button click move to another screen

From Dev

React Native: Unable to render components in a row with flex box

From Dev

react toggle menu, styling the button different on click

From Dev

Facebook React to render different components based on url hash

From Dev

How do I render a component when a button is clicked in React Native

From Dev

How to display extra components in view on Button press in react-native?

From Dev

How to change screen in react native with react-navigation on click of a button?

From Dev

rendering react components on click

From Dev

How to validate email and password on click of submit button in React native?

From Dev

React native:how to get Text value on a button click?

From Dev

How to dynamically Load API Data on a button Click in React Native

From Dev

Render HTML in React Native

From Dev

React Native staggered render

From Dev

React Native View Render

From Dev

React Native: Render on Fetch

From Dev

how to render different components using react-router-dom v6.0.0 with react-redux

From Dev

React Native - Components not loading

From Dev

Render partial view on button click

From Dev

Render multiple components in React Router

Related Related

  1. 1

    React-native, render a button click dynamically

  2. 2

    React-native, render a button click dynamically

  3. 3

    Is there a better or more elegant way to conditionally render different components in React Native?

  4. 4

    Not sure how to render this component with click of a button in react-native

  5. 5

    How to render components using function in React Native

  6. 6

    react.js render components at different locations

  7. 7

    Duplicate components in React after click button

  8. 8

    How to navigate to different screen on button click in react-native android app using react-navigation

  9. 9

    Render a component onPress(TouchableOpacity/Button) in React Native

  10. 10

    Signleton between different components in React Native

  11. 11

    React native button click move to another screen

  12. 12

    React Native: Unable to render components in a row with flex box

  13. 13

    react toggle menu, styling the button different on click

  14. 14

    Facebook React to render different components based on url hash

  15. 15

    How do I render a component when a button is clicked in React Native

  16. 16

    How to display extra components in view on Button press in react-native?

  17. 17

    How to change screen in react native with react-navigation on click of a button?

  18. 18

    rendering react components on click

  19. 19

    How to validate email and password on click of submit button in React native?

  20. 20

    React native:how to get Text value on a button click?

  21. 21

    How to dynamically Load API Data on a button Click in React Native

  22. 22

    Render HTML in React Native

  23. 23

    React Native staggered render

  24. 24

    React Native View Render

  25. 25

    React Native: Render on Fetch

  26. 26

    how to render different components using react-router-dom v6.0.0 with react-redux

  27. 27

    React Native - Components not loading

  28. 28

    Render partial view on button click

  29. 29

    Render multiple components in React Router

HotTag

Archive