Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native SectionList: What are the correct TypeScript types

I'm building a React Native app using TypeScript. I'm trying to use a SectionList. I followed the docs, and here is my code:

  renderSectionHeader = ({ section: { title } }: { section: { title: string } }) => (
    <ListItem title={title} />
  );

  render() {
    const { sections } = this.props;
    return (
      <SafeAreaView style={styles.container}>
        <SectionList
          keyExtractor={this.keyExtractor}
          sections={[
            {title: 'Title1', data: ['item1', 'item2']},
            {title: 'Title2', data: ['item3', 'item4']},
            {title: 'Title3', data: ['item5', 'item6']},
          ]}
          renderItem={this.renderItem}
          renderSectionHeader={this.renderSectionHeader}
        />
      </SafeAreaView>
    );
  }

But the line renderSectionHeader={this.renderSectionHeader} throws the following TSLint Error:

[ts]
Type '({ section: { title } }: { section: { title: string; }; }) => Element' is not assignable to type '(info: { section: SectionListData<any>; }) => ReactElement<any> | null'.
  Types of parameters '__0' and 'info' are incompatible.
    Type '{ section: SectionListData<any>; }' is not assignable to type '{ section: { title: string; }; }'.
      Types of property 'section' are incompatible.
        Type 'SectionListData<any>' is not assignable to type '{ title: string; }'.
          Property 'title' is missing in type 'SectionListData<any>'. [2322]

Are the types of SectionList broken? Or is the example wrong? Or am I doing something wrong?

like image 290
J. Hesters Avatar asked Dec 09 '18 11:12

J. Hesters


People also ask

What is the difference between FlatList and SectionList?

SectionList s are like FlatList s, but they can have section headers to separate groups of rows. SectionList s render each item in their input sections using the renderSectionHeader and renderItem prop. Each item in sections should be an object with a unique id (the key), and an array data of row data.

Is react native Javascript or TypeScript?

While React Native is built in Flow, it supports both TypeScript and Flow by default.


2 Answers

interface Data {
...
}

const MySectionList = SectionList as SectionList<Data>;

<MySectionList
...
/>

worked for me

like image 150
user11847380 Avatar answered Oct 03 '22 06:10

user11847380


[
  { title: "Title1", data: ["item1", "item2"] },
  { title: "Title2", data: ["item3", "item4"] },
  { title: "Title3", data: ["item5", "item6"] },
]

first you need to define the Section Type & the date item Type.

I'll use an interface here to define them.


type Item = string

interface Section {
  title: string;
  data: Item[]
}

now you need to define your `SectionList` `renderItem` function

const renderItem: SectionListRenderItem<Item, Section> = ({ item }) => {
  // make sure you return some sort of a component here.
};

like image 22
Ahmed Eid Avatar answered Oct 03 '22 05:10

Ahmed Eid