2

I have an array that I need to map out in <Text>. This is what I've got in render()

categories.map(category => <Text>{category.testHeader}</Text>

But it doesn't print anything. I guess <Text> needs to be in render() right? So I tried adding it in a function to be called in render. Like this:

function myfunc() {
    return categories.map(category => <Text>{category.testHeader}</Text>)
}

Then in render():

<View>
    {myfunc()}
</View>

But then the compiler said `Cannot read property 'map' of undefined. SO tips told me to put write:

function myfunc() {
    if (this.props.data) {
        return categories.map(category => <Text>{category.testHeader}</Text>)
    }
}

But now the compiler tells me that data is undefined. Not sure what to do here... :/

2
  • 2
    is categories a global variable? did you mean this.props.categories or this.props.data.categories? Commented Apr 9, 2018 at 8:11
  • categories, where it come from ? should be props, local state, private variable, etc. do you mean this.props.data.categories? Commented Apr 9, 2018 at 8:46

2 Answers 2

7

You can use like this:

var categories = [{ id: 0, text: 'hasan' }, 
    { id: 1, text: 'erkan' },
    { id: 2, text: 'veli' }];

export default class App extends Component {
renderCategories() {
    return categories.map((item, index) => <Text key={index}>{item.text}</Text>);
}
render() {
    return (
        <View style={styles.container}>
            {this.renderCategories()}
        </View>
    );
}

}

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

Comments

0

Firstly defined the data to be []. Then assign categories array to data and try to use it. If required use JSON.parse(data)

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.