0

I'm trying to print an Array whose name is being calculated based on selected value. e.g.

const ANotes = ["B", "C", "D"];
const keyNotes = MyNotes[selected] + "Notes"; //value is ANotes

Trying to render it as:

<View>
        {{keyNotes}.map((item, key)=>(
         <Text key={key} 
          //style={styles.TextStyle} 
          //onPress={ this.SampleFunction.bind(this, item) }
          > { item } , </Text>)
        )}
      </View>

When I try to render it in View in my Component it gives error. I'm new to React Native so not able to figure this one. I want the output to show B, C, D in the App.

It gives error: TypeError: undefined is not a function.

1 Answer 1

0

You cant use a string value to access a variable in the local scope without it being attached to some object or array check out this SO for some solutions

Seems you're using functional component if you switch to a class component you can then access it with this[keyNotes]

<View>
     {this[keyNotes].map((item)=><Text key={item}> {item}Notes,  </Text>)} 
</View>

Here's a working sandbox (I'm using react not react-native so I changed to dom eelments)

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

7 Comments

keyNotes is the selected value that holds the string ANotes. I used this as an example.
What do you mean the "string ANotes"? ANotes is an array. And what's MyNotes?
MyNotes[selected] evaluates to "A" which makes keyNotes evaluate to "ANotes". It's mentioned in the code snippet comment part.
So KeyNotes is the string 'ANotes'?
Yes. So if I use keyNotes to fetch array values of ANotes. What should be the exact syntax?
|

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.