2

I'm trying to create a small comment box using react.

My user will input into a name and comment box.

This gets saved as an array and added to the existing state array.

The updated data gets mapped into a table and displayed.

I can see the updated arrays in my console, but nothing is displayed. What have I done wrong?

    import '../App.css';
import React, { useState } from 'react';

function App(props) {
  const [userName, setUserName] = useState("");
  const [userComment, setUserComment] = useState("");
  const [commentArray, setCommentArray] = useState([]);

  const commentWrite = (event) => {
    event.preventDefault();
    let comment = [
      {
        name: `${userName}`,
        comment: `${userComment}`
      }
    ];
    setCommentArray([...commentArray, comment]);
    console.log(commentArray);
  };


  const comments = commentArray.map((comment, index) => {
    return (
        <tr key={index}>
            <td>{comment.name}</td>
            <td>{comment.comment}</td>
        </tr>
    )
});

  return (
    <>


      <div className="wrapper">
        <header className="App-header">
          <h1>Comment box</h1>
        </header>
        <div className="commentInput">
          <form >
            <input type="text" placeholder="Name" required onChange={e => setUserName(e.target.value)}></input>
            <input type="text" placeholder="Comment" required onChange={e => setUserComment(e.target.value)}></input>
            <button type="submit" onClick={(event) => { commentWrite(event, userName, userComment) }} >Post</button>
          </form>
        </div>

        <table>
          <thead>
            <tr>
              <th>Name</th>
              <th>Comment</th>
            </tr>
          </thead>
          <tbody>
            {comments}
          </tbody>
        </table>

      </div>
    </>
  );
}

export default App;

3 Answers 3

2

When you add a new comment to the comment array, you are adding it as an array of one object. This results in your comment array having a nested array, however your rendering of the comment array expects objects inside of it.

You probably want to rewrite the comment code to create an object and not an array of a single object:

let comment = {
    name: userName,
    comment: userComment
};
Sign up to request clarification or add additional context in comments.

Comments

0

That id because you are contacting the wrong thing in your function commentWrite. You are appending array inside an array. The solution will be :

  const commentWrite = (event) => {
    event.preventDefault();
    let comment = [
      {
        name: `${userName}`,
        comment: `${userComment}`
      }
    ];
    setCommentArray([...commentArray, ...comment]);
    console.log(commentArray);
  };

Or you can do one thing is Make comment as JSON object

  const commentWrite = (event) => {
    event.preventDefault();
    let comment = {
        name: `${userName}`,
        comment: `${userComment}`
      };
    setCommentArray([...commentArray, comment]);
    console.log(commentArray);
  };

Comments

0

Here is the solution for you

import '../App.css';

import React, { useState } from 'react';

function App(props) {
  const [userName, setUserName] = useState("");
  const [userComment, setUserComment] = useState("");
  const [commentArray, setCommentArray] = useState([]);

  const commentWrite = (event) => {
    event.preventDefault();
    let comment = [
      {
        name: userName,
        comment: userComment
      }
    ];
    setCommentArray([...commentArray, comment]);
 
  };


  const comments = commentArray.map((comment, index) => {
    return (
      <tr key={index}>
            <td>{comment[0].name}</td>
            <td>{comment[0].comment}</td>
        </tr>
  )
});

  return (
    <>


      <div className="wrapper">
        <header className="App-header">
          <h1>Comment box</h1>
        </header>
        <div className="commentInput">
          <form >
            <input type="text" placeholder="Name" required onChange={e => setUserName(e.target.value)}></input>
            <input type="text" placeholder="Comment" required onChange={e => setUserComment(e.target.value)}></input>
            <button type="submit" onClick={(event) => { commentWrite(event, userName, userComment) }} >Post</button>
          </form>
        </div>

        <table>
          <thead>
            <tr>
              <th>Name</th>
              <th>Comment</th>
            </tr>
          </thead>
          <tbody>
            {comments}
          </tbody>
        </table>

      </div>
    </>
  );
}

export default App;

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.