How to transfer props to a styled component in ReactMarkdown`s syntax

I am attempting to display some links using the ReactMarkdown library within a React component, but in order to achieve a specific styling, I need to pass certain props.

The Links component is a styled component that I am using to style the paragraph property within the renderers object of ReactMarkdown. However, to make Links work properly, I must pass the linkColor={linkColor} props.

Code: A snippet of my code where Container is another styled div:

 <Container>
    {websites.map((website, index) => (
      <div key={'website' + index}>
        <ReactMarkdown
          source={`[${website.websiteName}](${website.externalUrl})`}
          unwrapDisallowed={true}
          renderers={{ paragraph: Links, link: Linkrender }}
        />
      </div>
    ))}
  </Container>

  const Links = styled.div`
   color: ${(p) => p.linkColor};

   ::before {
     content: ' ';
     white-space: pre;
   }

   ::after {
      content: '  /';
      white-space: pre;
   }
  `;

Tried: I attempted the following solution, but it didn't work as expected. It resulted in the source of ReactMarkdown being lost and only the style applied to an empty div.

 <ReactMarkdown
          source={`[${website.websiteName}](${website.externalUrl})`}
          unwrapDisallowed={true}
          renderers={{ paragraph: ({linkColor}) => (
                         <Links {...linkColor={linkColor}} />),  <<<<<<<<<<<<< tried this
                       link: Linkrender }}
        />

Is the syntax correct? What could be causing this issue? Could it be because my styled component is a div being applied to a <p>? Additionally, Linkrender is another styled component that is custom-made and is an <a> element that cannot be altered.

Answer №1

An effective approach I found was to use a styled.div as a container and add custom styling to the paragrah within ReactMarkdown. Follow the instructions indicated by the arrows in the code snippet.

  <Container>
    <Heading>{`${textStripeHeading}:`}</Heading>
    {websites.map((website, index) => (
      <Links key={'website' + index} linkColor={linkColor}>     <<<<<<<<<<< add <Links> here
        <ReactMarkdown
          source={`[${website.websiteName}](${website.externalUrl})`}
          unwrapDisallowed={true}
          renderers={{ paragraph: MarkdownStyle, link: Linkrender }}   <<<<<<<<< create new style.p with style and apply to paragraph
        />
      </Links>
    ))}
  </Container>

   const MarkdownStyle = styled.p`
      ::before {
        content: ' ';
        white-space: pre;
      }

     ::after {
      content: '  /';
      white-space: pre;
     }
   `;

This method proved to be effective, but I am open to exploring other solutions that may offer improvements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sliding Feature

Can someone assist me with implementing a jQuery half slide function from left to right? Please check out the following URL for more information: http://jsfiddle.net/prabunivas/Fy9Hs/2/ <div> <div id="col1" style="float:left"> &l ...

Async Autocomplete fails to display options if the label keys do not match the filtering keys

While I have experience with ReactJs and Material UI, I encountered a surprising issue. I am using the Material-UI Autocomplete component as shown below. The users variable is an array of objects. When searching for firstName or lastName in the user table, ...

Webix UI - Struggling to create dynamically responsive components during screen resizing

Can anyone guide me on how to make my Webix UI control responsive in the free version available at ? It seems to be acting unpredictably due to the AngularJS integration code I'm using. I have attempted various solutions like media queries, redraw, a ...

Checking if a useState setter passed in props is triggered in a Jest test

In a React component, I have the following functions: const changeOrder => { a websocket request }; const handleArrow = async () => { const res = await changeOrder(side, headingOrder, config); if (res !== undefined) setOrder(res); }; The se ...

Tips for updating the appearance of a specific column in React Native

I've been working on creating a matrix-style table design to show available seats in a bus! I'm iterating through 2D and 3D arrays to achieve this layout! Here is an image of the current output: https://i.sstatic.net/oNtKI.jpg In the image, yo ...

Centralize the X button using CSS

My latest challenge involves creating a close button using CSS to close a tag. I combined techniques from an article on Patterns for Closebuttons, along with a css-only tutorial for crafting an X from this Close Button using CSS. However, I've run in ...

Building a loading spinner component in a react-native project

I have successfully implemented a loading spinner that is currently being used in various components of my React project. However, as I am now working on a react-native version of the application, I am faced with the challenge of recreating this loading sp ...

Disabling the visibility of elements through a transparent sticky-top menu

I'm having an issue with my website design. I have a gradient background and a sticky-top menu on the site. The problem is that when I scroll down, the content appears through the menu, which is not ideal. I don't want to apply the same gradient ...

What could be causing the text field in a reactjs component to remain blank despite setting a defaultValue while making an API call?

My code isn't showing up correctly, I have the following: <TextField size="small" className="typing-container" defaultValue={thing.thingLastName} label="Last N ...

Is it feasible to adjust the positioning of invalid tooltips?

I've been trying to figure out how to adjust the position of my invalid tooltips to move a bit to the right and match my input position, but I haven't had any success yet. Here's the image I'm referring to: https://i.sstatic.net/fovSS. ...

Securing Routes in React: Issues with Server-side JWT Token Validation

A special project was created to focus on Authentication and Security improvements. Despite efforts, the desired results are not being achieved. Using a Node.js backend with mongoDB and a React frontend, progress has been made in sending a Jwt token from t ...

Extension for Chrome that uses React technology to enhance router functionality

I'm facing an issue in my Chrome extension development using React. I keep getting a warning that says: No routes matched location "/popup.html", which is preventing the app from starting. To build this app, I followed this particular templ ...

The enigmatic void nestled amidst two dividers

I designed a layout with two buttons beside a progress bar, where the buttons are enclosed within a <div> element: <div class="block"> <div class="progress progress-striped active"> <div class="progress-bar" role="progress ...

Creating a unique custom row labeled "ALL" at the top of an ag-grid, allowing it to be selected just like any other row

Looking for assistance regarding a customization in my ag-grid setup. The table consists of two columns - one for text and the other for numbers. I am trying to include a unique row at the very top of the ag-grid table that displays the total value of all ...

Unable to adjust iframe height

Below is a snippet of code that I am working with: <style type="text/css"> .div_class { position:absolute; border:1px solid blue; left:50%; margin-left:-375px; margin-top:100px; height:300px; width:500px; overflow:hidden; } .iframe_class { position: ...

so many alerts to choose from aside from jsx/react

After some consideration, I believed I had devised an effective way to notify users when the array is empty and there's nothing to display. However, upon implementation, I realized my technique only works onFocus or page reload because I placed the fu ...

Is NextJS on-demand revalidation compatible with Next/link?

https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration#on-demand-revalidation According to the NextJS documentation, it appears that on-demand cache revalidation should function properly with regular <a> tags and when t ...

React CSS Modules - Adding styles to individual child elements

I'm having difficulty applying CSS styles to each child of a specific tag. I am using CSS modules in conjunction with semantic-ui-react: character.module.css .Character > GridColumn { border: solid 4px red; } character.js import React, {Com ...

What is the best approach for incorporating sub-navigation within a page popup in a Next.js project?

In the midst of my Next.js project, there is a requirement to showcase a chat popup consisting of multiple sub-pages like user registration, conversation page, and more. Hence, seamless navigation inside this popup is necessary. The idea is not to alter th ...

Explore creating a dial number using React

Hey there, I'm currently working on developing a component. Just to clarify, I am not using react-native for this project. Instead, I would like to utilize React to scroll a number similar to how an image scrolls. https://i.sstatic.net/c1Tu8.png Th ...