usman9023's avatar

I want to add Read More Functionality in React Js

Im new in React js and I want to add Read More Functionality in React Js. im fetching title and its body on two pages i.e titles on 1 page and its body description in another page/route etc . But I want whenever i click on title its whole description along with title open in another page.and Im using DummyJsonAPi. ->https://dummyjson.com/docs/posts

Here is my code where im fetching all the titles from dummyapi

import React, { useEffect, useState } from "react";
import { v4 as uuidv4 } from "uuid";
import { Link } from "react-router-dom";

const HomeMainContent = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    fetch("https://dummyjson.com/posts")
      .then((response) => response.json())
      .then((data) => {
        const postText = data.posts.map((postData) => {
          return { title: postData.title };
        });
         setData(postText);    
      });
  }, []);
  return (
    <React.Fragment>
      <div className="container px-4 px-lg-5">
        <div className="row gx-4 gx-lg-5 justify-content-center">
          <div className="col-md-10 col-lg-8 col-xl-7">    
            <div className="post-preview">
                {data
                  ? data.map((meaning) => {
                      return <h2 className="post-title" key={uuidv4()} ><Link to="/post">{meaning.title}</Link> <hr/> </h2>;
                    })
                  : null}
              
            </div>

            <div className="d-flex justify-content-end mb-4">
              <a className="btn btn-primary text-uppercase" href="#!">
                More Posts →
              </a>
            </div>
          </div>
        </div>
      </div>
    </React.Fragment>
  );
};

export default HomeMainContent;

And here is my code where im fetching all the body text from an dummy api

import React, { useEffect, useState } from "react";
import { v4 as uuidv4 } from "uuid";

const PostMainContent = () => {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch("https://dummyjson.com/posts")
      .then((response) => response.json())
      .then((data) => {
        const postText = data.posts.map((postData) => {
          return { body: postData.body };
        });
        setData(postText);
      });
  }, []);
  return (
    <React.Fragment>
      <article className="mb-4">
        <div className="container px-4 px-lg-5">
          <div className="row gx-4 gx-lg-5 justify-content-center">
            <div className="col-md-10 col-lg-8 col-xl-7">
              {data
                ? data.map((meaning) => {
                    return  <p key={uuidv4()}>{meaning.body} </p>;
                  })
                : null}
            </div>
          </div>
        </div>
      </article>
    </React.Fragment>
  );
};
export default PostMainContent;

Now i want to click any title and open its body description in another page . How can i achieve this . POV: i dont know REDUX . Thanks in advance

0 likes
3 replies
Sinnbeck's avatar

Do you mean open a new tab or like a modal?

and open its body description in another page

If its just a new tab in the browser, add some sort of url to your system that can handle showing the body description alone on a page, and use an <a tag

<a href={'/meaning/' + meaning.id} target="_blank">{title}</a>
1 like
usman9023's avatar

@Sinnbeck can you explain more briefly because i didn't know the terms but i will try to clear myself as you can see in DummyApi there is an array which contains different objects and each object contains id title and body . I want to show title in one page and via LINK(React Router DOM) whenever title is clicked new component is open and title and its all description shows their

Please or to participate in this conversation.