0

I'm trying to display a 2 dimensional string array in my react-native code, that is passed on from the native code. Passing the code is not a problem, however I don't really get how to properly display this array. For testing purposes, I made the message array a constant in the js code.

Here is my array:

const messageArray = [
  ["msg1", "date1"],
  ["msg2", "date2"],
  ["msg3", "date3"],
  ["msg4", "date4"],
  ["msg5", "date5"]
];

How do I render this into a view?

3
  • Render is as what? A table? Commented Jul 16, 2019 at 9:20
  • I think your post needs some more info. Where is the code? What did you try? Did you have any errors? etc. Commented Jul 16, 2019 at 9:20
  • All my attempts were complete failures, so it wouldt make any sense to post them. I basically just needed to render it as plain text. Commented Jul 16, 2019 at 11:14

2 Answers 2

1

Here's an example of how you can do it. You can use destructuring.

const messageArray = [
  ["msg1", "date1"],
  ["msg2", "date2"],
  ["msg3", "date3"],
  ["msg4", "date4"],
  ["msg5", "date5"]
];

class App extends React.Component {
  render() {
    return (
      <div>
        {messageArray && messageArray.map(([message, date]) =>
          <div className='msg-item'>
            message: {message || ''}
            <br />
            date: {date || ''}
          </div>
        )}
      </div>
    );
  }
}

// Render it
ReactDOM.render(
  <App />,
  document.getElementById("react")
);
.msg-item {
  border: 1px solid black;
}

.msg-item + .msg-item {
  margin-top: 1rem;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

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

1 Comment

Thanks for your response! this is however "normal" react code in comparison to react-native code, but after conversion it worked!
0

Solved it using the answer of jank, for anyone with the same issue:

const messageArray = [
  ["msg1", "date1"],
  ["msg2", "date2"],
  ["msg3", "date3"],
  ["msg4", "date4"],
  ["msg5", "date5"]
];

export default class App extends Component {

    Render(){
        <View>
            {messageArray && messageArray.map(([message, date]) => <View>
            <Text>message: {message || ''}</Text>
            <Text>date: {date || ''}</Text>
            </View>)}
       </View>

}

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.