React Native different categories rendered to header component

user8359832

I am developing a React Native quiz application. I already have developed the header component and it looks fantastic.

// import libraries for making a component
import React from 'react';
import { Text, View } from 'react-native';

// make a component
const Header = (props) => {
  const { textStyle, viewStyle } = styles;
  return (
    <View style={viewStyle}>
      <Text style={textStyle}>{props.headerText}</Text>;
    </View>
  );
};

const styles = {
  viewStyle: {
    backgroundColor: '#F8F8F8',
    justifyContent: 'center',
    alignItems: 'center',
    height: 60,
    paddingTop: 15,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.2,
    elevation: 2,
    position: 'relative'
  },
  textStyle: {
    fontSize: 20
  }
};

// make a component available to other parts of the app
export default Header;

I am going to be doing an ajax request to an api that has a category and then one quiz question per category.

When I make a GET request to this endpoint I get back an array of objects and each object has a question category and one boolean question of true or false.

So in total I have 10 categories with a question for each category for a total of 10 questions.

So I need to figure out how to make an Ajax request or an http request from my mobile application to fetch this list of data and more critically, once I have the list of data, I need to figure out a way to get the category piece to render as the header.

This is the part I am stuck on.

I plan to have a component called QuestionList, the purpose of QuestionList will be to fetch the list of data or fetch the list of questions and once fetched, it will render several QuestionDetail components. So I will have a QuestionList and a QuestionDetail.

But again the api has a category for each question and I want to render that category content as the header for each question. So a different header each time the user goes to the next question.

So in the code above, rather than letting the header component decide what text should be displayed I refactored it slightly so that the app component will decide what text to show in there.

Did I just mess this up in terms of what I am trying to accomplish? Should I have let the header component decide what text should be displayed? And more importantly how do I get the header component or the App component to render the different categories? Should I be creating a separate CategoryList component and then a CategoryHeader component for each specific component that renders a different category from the api?

Pritish Vaidya

Based on your requirements you need a SectionList.

It already has a support for renderSectionHeader where you can integrate your Header component

An example to that would be

<SectionList 
  renderItem={({ item, index, section }) => <QuestionDetails {...item} />} // Render your Question Details Component here
  renderSectionHeader={({ section: { title } }) => <Header {...title}/>} //... Render your Custom Header Component here 
  sections={[ 
   { title: 'Category1', data: ['question1', 'question2'] }, 
   { title: 'Category2', data: ['question1', 'question2'] }, 
   { title: 'Category3', data: ['question1', 'question2'] }, 
   ]} 
  keyExtractor={(item, index) => item + index} />

This is just a sample, you can modify to generate the array based on this that suits your requirements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React-navigation with react-native: DrawerIcon not rendered if part of component

How to update the header while the component is still rendered using React Navigation?

React Native - Text must be rendered withing a <Text> component

React Native: Scene/Component not being rendered when returned fromm renderScene

Show ActivityIndicator over current rendered component React Native

React Native onLongPress triggered onPress if pressed component is re-rendered

react native runs component code while it's not rendered

React Native Text strings must be rendered within a <Text> component

react native Error: Text strings must be rendered within a <Text> component

React component not rendered in loop

React component is not being rendered

React Native: change State from different component

React Native close Modal that is opened by different component

Share data in different component in react native

How to render one React Native component after another component had rendered?

iOS (React native): Unnecessary space from the top of the header rendered using react navigation

what is the different between class component and function component - React Native

React component is getting rendered twice

React component Game not rendered with route

Pagination Component not rendered on UI - React

React dynamically rendered component not updating

React component being re rendered

React Router | Component not getting rendered

Child Component not rendered in react js

Child Component Not Being Rendered - React

Add a component at the bottom of navigationOptions header of React-Native

Passing title from React Native navigation to Header Component

React Native: Image is not being rendered

How can a react-native component be rendered inside a fetch success response?