2

I am trying to output the data inside render() but I am getting error why so ?

Code:

render() {

        let marketing_plan_projects = [];

        marketing_plan_projects = this.props.markets.data ? this.props.markets.data : null;
        let marketing_updates = get_marketing_updates_from_projects(marketing_plan_projects);

        console.log(marketing_updates);

        return (
            <ScrollView style={styles.container}>

                <Text>{marketing_updates[0].project_name}</Text>

            </ScrollView>
        );
    }

I also tried this: (still gives error)

<Text>{marketing_updates ? marketing_updates[0].project_name: ""}</Text>

console.log(marketing_updates) is :

enter image description here

I am getting following error:

enter image description here

4
  • Are you sure your marketing_updates object is not empty ? Commented Oct 1, 2018 at 9:49
  • @R.Duteil Initially it is empty(see in screenshot) but then re rendering happens so it gets populated. Commented Oct 1, 2018 at 9:51
  • 1
    Your error is thrown because at first you have an empty array. react-native can't know that at some point you will actually have data there. You could do what you have already tried with some modifications: <Text>{marketing_updates && marketing_updates.length > 0 ? marketing_updates[0].project_name: ""}</Text> Commented Oct 1, 2018 at 9:58
  • @AndreiOlar Works like a charm thanks :) Commented Oct 1, 2018 at 10:08

1 Answer 1

2

This line :

marketing_updates ? marketing_updates[0].project_name: ""

does not test if the array is empty, but just if it exists. Testing marketing_updates = [] will return true, as it is a truthy value.

See here for more informations : https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Thus you can have an existing array but an undefined value for marketing_updates[0] as it might be empty.

To avoid your error, try this instead:

marketing_updates.length != 0 ? marketing_updates[0].project_name: ""
Sign up to request clarification or add additional context in comments.

5 Comments

If I want to inject the data into FlatList how can I do that ? Is this correct -> data={marketing_updates && marketing_updates.length > 0 ? marketing_updates[0].project_name: null }
This will send marketing_updates' first element's project_name if there is at least one element, null otherwise as data.
So I am just asking is this correct way -> data={marketing_updates && marketing_updates.length > 0 ? marketing_updates[0].project_name: null } to pass data to FlatList considering the situation in my question.
It depends on what you need to pass as data to your FlatList. If you need the project_name of the first element, then yes, it is the correct way

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.