<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blaine Yilmaz</title>
    <description>The latest articles on DEV Community by Blaine Yilmaz (@blaineyilmaz).</description>
    <link>https://dev.to/blaineyilmaz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1253968%2F7f407ece-c1e5-471d-a052-ae76c4ce74f7.jpg</url>
      <title>DEV Community: Blaine Yilmaz</title>
      <link>https://dev.to/blaineyilmaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blaineyilmaz"/>
    <language>en</language>
    <item>
      <title>How to create a forum in React</title>
      <dc:creator>Blaine Yilmaz</dc:creator>
      <pubDate>Thu, 11 Jan 2024 07:39:20 +0000</pubDate>
      <link>https://dev.to/blaineyilmaz/how-to-create-forum-in-react-37of</link>
      <guid>https://dev.to/blaineyilmaz/how-to-create-forum-in-react-37of</guid>
      <description>&lt;p&gt;First, create a project with Vite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After clean up, the project tree should looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── client
  ├── node_modules
  ├── package.json
  ├── package-lock.json
  ├── public
  ├── src
    ├── App.jsx
    ├── index.css
    └── main.jsx
  ├── .gitignore
  ├── index.html
  └── vite.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add this boilerplate to App.jsx. This should print the input values at the browser console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";

function NewTopic() {

  const [title, setTitle] = useState(String);
  const [content, setContent] = useState(String);

  const Submit = (e) =&amp;gt; {
    e.preventDefault();

   console.log(title, content)
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;form onSubmit={Submit}&amp;gt;
        &amp;lt;input onChange={(e) =&amp;gt; setTitle(e.target.value)} value={title} /&amp;gt;
        &amp;lt;textarea onChange={(e) =&amp;gt; setContent(e.target.value)} value={content} /&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Create&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

export default NewTopic;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For sending the input data to a server, we'll use Axios library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';

const Submit = (e) =&amp;gt; {
    e.preventDefault();

    try {
      axios.post('http://localhost:3001/createPost', { title, content })
    } catch (error) {
      console.log(error);
    }
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a Node server with Express.js library:&lt;/p&gt;

&lt;p&gt;server.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cors = require('cors');
const express = require('express');
const app = express();
const server = require('http').createServer(app);

app.use(cors());
app.use(express.json());

server.listen(3001);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The current project tree should looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── .gitignore
├── index.html
├── package.json
├── package-lock.json
├── public
├── server.js
├── src
│   ├── App.jsx
│   ├── index.css
│   └── main.jsx
└── vite.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep our input data in one place, we'll use Mongo Database.&lt;br&gt;
To get your own Mongo Database URL fallow &lt;a href="https://www.mongodb.com/cloud/atlas/register"&gt;this url&lt;/a&gt;.&lt;br&gt;
Add all the Mongoose library functionalities to the server.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const { Schema } = mongoose;

mongoose.connect("mongodb+srv://username:password@cluster0.ufesskg.mongodb.net/forum-database");

const databaseSchema = new Schema({
  title: String,
  content: String
});

const Post = mongoose.model('Post', databaseSchema);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to give a response to coming request from React-side, create an endpoint in server.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/createPost', (req, res) =&amp;gt; {
  Post.create({
    title: req.body.title,
    content: req.body.content
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Screenshot from &lt;a href="https://cloud.mongodb.com"&gt;https://cloud.mongodb.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ofu718y6ufelxfhha4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ofu718y6ufelxfhha4q.png" alt="" width="442" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code is just creates post. Now, return all posts to React-side for creating a simple forum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/createPost', (req, res) =&amp;gt; {
  Post.create({
    title: req.body.title,
    content: req.body.content
  });

  Post.find().then(PostFind =&amp;gt; {
    var allData = PostFind;

    return res.json({ allData });
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, back to the React-side to fetch &lt;code&gt;res.json({ allData });&lt;/code&gt; response with Axios.&lt;/p&gt;

&lt;p&gt;We'll use Javascript map function, because we must list all the data to line down as column.&lt;/p&gt;

&lt;p&gt;The final React-side code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";
import axios from 'axios';

const NewTopic = () =&amp;gt; {

  const [title, setTitle] = useState(String);
  const [content, setContent] = useState(String);
  const [allData, setAllData] = useState(null);

  const Submit = (e) =&amp;gt; {
    e.preventDefault();

    try {
      axios.post('http://localhost:3001/createPost', { title, content }).then(TheResponse =&amp;gt; {
        setAllData(TheResponse.data.allData);
      });
    } catch (error) {
      console.log(error);
    }
  };

  return (
    &amp;lt;&amp;gt;
      &amp;lt;form onSubmit={Submit}&amp;gt;
        &amp;lt;input onChange={(e) =&amp;gt; setTitle(e.target.value)} value={title} /&amp;gt;
        &amp;lt;textarea onChange={(e) =&amp;gt; setContent(e.target.value)} value={content} /&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Create&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;

      &amp;lt;div&amp;gt;
        {
          allData.map((mapAllData) =&amp;gt; (
            &amp;lt;div&amp;gt;
              &amp;lt;p&amp;gt;Title: {mapAllData.title} ||||||||||| Content: {mapAllData.content}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          ))
        }
      &amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

export default NewTopic;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf8ahp2ue2dw5vmfvnar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf8ahp2ue2dw5vmfvnar.png" alt="" width="650" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you'll have encounter an error, remove the &lt;code&gt;"type": "module",&lt;/code&gt; line from the &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget running both of express server and React-side client:&lt;br&gt;
&lt;code&gt;node server.js&lt;/code&gt;&lt;br&gt;
and for React:&lt;br&gt;
&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Blainegith/devto-react-forum"&gt;https://github.com/Blainegith/devto-react-forum&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
