0

I have a FlatList in React Native -

Here's my object that I'm passing on to FlatList.

    const [workoutSet, setWorkoutSet] = useState([
        { setNum: 1, reps: 10, weight: 0 },
        { setNum: 2, reps: 10, weight: 0 },
    ]);

    <SetList data={workoutSet} />

Within SetList I'm trying to render the data passed on to here.

export default function SetList(data) {
    return (
        <>
            <FlatList
                data={data}
                renderItem={(item) => {
                    <Text style={styles.name}> {item} </Text>;
                }}
            />
            {console.log(data.data[0].reps)}
        </>
    );
}

console.log(data.data[0].reps) returns 10, as expected.

However, if I do a console.log inside renderItem, it doesn't print anything. I'd like to access setNum, reps and weight inside renderItem so that I can display a list. What am I doing wrong? I've searched StackOverflow and couldn't find an answer to this. Thanks.

6
  • data={data} should be data={data.data}. Or alternatively you can destructure in the function parameter: function SetList({ data }) (If you don't destructure, it's more conventional to call the argument props. Consistent naming is important to avoid confusion.) Commented Feb 14, 2021 at 11:06
  • make it like this and see what items is if you items is an object you will not see it on phone---like item.reps --- renderItem={(item) => {console.log("item");return( <Text style={styles.name}> {item} </Text>) }} Commented Feb 14, 2021 at 11:16
  • Ok i've changed data to props function SetList(props). Then I set data = {data.data}. But it's not returning anything. :( Commented Feb 14, 2021 at 11:16
  • <Text></Text> component doesn't show objects Commented Feb 14, 2021 at 11:18
  • I can't even do a console.log(item) inside renderItem, which seems to be the issue here. Commented Feb 14, 2021 at 11:23

1 Answer 1

1

Working example items in your render is an object thats why it is not showing text component don't show object as text i recommend reading about flatlist to understand more about it -key extractors -layouts

like best practices since it can be a problem for large lists (very slow performance for 100 rows)

working example https://snack.expo.io/LpItecGOc

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

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.