1

I have been struggling with this code for a while now. I tried using array map to display content in react native view but keeps throwing errors which I already handled.

function AppSlim() {
  //const todos = useSelector(state=>state.TodoList).list; 
  const todos = ['Hello', 'Michael']
  console.log('slim todos',todos);
  return (
    <View style={styles.container}>
      
      <MyBtn />
      <MyModal />
      <View>
        {/* {alert("hi")} */}
        {console.log('todos',todos)}
        {todos.map((item)=>(<Text key={item}>{item}</Text>))};
        {/* <Text>Hi</Text>
        <Text>Mike</Text> */}
        
      </View>
    </View>
  );
}

I have been battling with this issue for quite some time and I can't see what is wrong with my code. And I get the error:

Text strings must be rendered within a <Text> component.

What I'm I missing please?

2 Answers 2

2

The error occurs due to the semicolon after the closing curly bracket. The App tries to render the ; as Text, but it's missing the <Text> Component for it.

Change this:

{todos.map((item)=>(<Text key={item}>{item}</Text>))};

To:

{todos.map((item)=>(<Text key={item}>{item}</Text>))}

Demo:

https://snack.expo.io/rJw5K8oL8

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

Comments

0

try this code Don't forget to return the mapped array, like:

 return todos.map((data) => {
          return (
            <View><Text key={item}>{item}</Text></View>
          )
        })

Reference for the map() method: link

1 Comment

That's not the problem.

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.