Managing image elements as attributes in React using styled-components

In my React project, I am working towards eliminating the use of .css files. Currently, I am refactoring existing .css code to utilize styled-components in a Typescript React project.

Previously, images were included as backgrounds in a .css class

a.item.overview {
  background: url(../img/overview.svg) no-repeat 16px 50% / 20px 20px;
  margin-top: 20px;
}

My approach for handling images involves converting the .svg files into react components.

import React from 'react'
import styled from 'styled-components'
import Icon from './Icon'

const Svg = styled(Icon)` 
    margin-top: 20px;
    fill: red;
    height: 20px;
    width: 20px;
    fill-rule:evenodd;
    clip-rule:evenodd;
`

export const OverviewIcon = () => ( 
    <Svg viewBox="0 0 20 20">  
        <path     
            d="X4.2,2.5k11.2c0...."
        />  
    </Svg>
)   

Now, I am aiming to create a component that will allow me to pass my image component as a property.

My goal is to have a single menuItem component where I can simply pass an image component and add text. Similar to the example below. https://i.sstatic.net/gNKSJ.png

As I am new to React, I am open to feedback on whether my approach is incorrect or too complicated.

I tried the following approach but encountered issues:

type Props = {
    image: React.ReactNode
}

export const LMenuItem = ({image}: Props) => (
    <div>{image}</div>
)

Answer №1

After trying numerous strategies, this method ultimately proved successful.

interface Props {
    text: string
  }

export const CustomComponent = ({text}: Props) => (
    <div>{text}</div>
)

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

Using Angular 2 to create an Observable type that mediates two separate http.get calls

In my ng2 service, I have a method that contains two http.get calls. Here is an example of the function: getInfo(userId: number): any { this.http .get(apiUrl, options) .map(response => response.json()) .subscribe(example => ...

In React/JavaScript, the array variable defined outside the component is having props added to it multiple times

After entering new data, I want to add it to an array called DUMMY_MEALS outside the component and display it as a list. The issue I am facing is that the 'data' object is being added multiple times to DUMMY_MEALS and rendering duplicates on the ...

Is there a way to keep my footer fixed to the bottom of the page in Google Chrome?

I recently made some updates to my aging website by adding a footer menu. However, I encountered an issue with the placement of the footer on Google Chrome. It appears too high, overlapping certain elements of the site. I attempted to resolve this problem ...

Detecting a click on the background div in Angular without capturing clicks on child divs

Over at ollynural.github.io, I've been working on creating a pop-up div in the portfolio page that provides additional information about the project you select. To close the pop-up, I have implemented an ng-click feature so that clicking on the main p ...

Angular struggles to process the mapped type modifiers in Typescript during compilation

In my node.js project, I have successfully implemented a custom mapped type as follows: export type Mutable<T> = { -readonly [P in keyof T]: T[P]; }; However, when I tried to add the same code to an Angular 6 project, compilation failed with th ...

The test suite encountered an error: Invariant violation occurred because the statement "Buffer.from("") instanceof Uint8Array" was evaluated as false when it should have been

**Error: The condition "Buffer.from("") instanceof Uint8Array" is incorrectly evaluating to false This error indicates a problem with your JavaScript environment. eBuild relies on this specific condition which suggests that your JS environment is not funct ...

Varying outcomes for identical types in TypeScript

I'm puzzled as to why TypeScript offers different suggestions for equal types (ObjType1 and ObjType2). In the first case, it suggests a union type "a" | "b" However, in the second case for the same type, it does not provide a uni ...

What is the best way to incorporate the useSearchParams hook into props or function arguments?

When I utilize the useSearchParams hook in my component, I encounter an issue where I am unable to type parameters when passing the hook to props or function arguments. import { useSearchParams } from 'react-router-dom'; const urlSearchParamState ...

The battle between CSS and jQuery: A guide to creating a dynamic zebra-style table without relying on additional plugins

Is there a better way to create a dynamic zebra styling for table rows while still being able to hide certain elements without losing the styling? In this code snippet, I'm using the CSS :nth-of-type(even) to style even rows but running into issues wh ...

Tips for altering the appearance of a button when moving to a related page

I have a master page with four buttons that have a mouse hover CSS property. Each button's corresponding response page is defined on the same master page. Now, I want to change the button style when the user is on the corresponding page. How can this ...

Accessing and manipulating web elements using either XPath or CSS selectors

Having trouble selecting a specific piece of information using xpath or css selector. Error messages keep appearing. Can someone help identify the issue? This snippet is from my code: output = driver.find_element_by_xpath("//td[@class_= 'sku']" ...

Include the request model as a parameter

I'm encountering an issue when trying to pass a model as a parameter to an API, resulting in the following error: Type 'string' is not assignable to type 'AVAFModel'. submitAvafDetails(): Observable<any> { this.avafBcmsV ...

What is the best way to utilize the typescript module for detecting and managing typescript errors and warnings in your code?

Currently, I am experimenting with the typescript module to programmatically detect typescript errors. Below is a simplified version of what I have been working on: var ts=require('typescript') var file_content=` interface Message{ a:string ...

Utilize MaterialUI's stepper component to jazz up your design with

Is there a way to customize the color of a material ui Stepper? By default, the material UI stepper's icons use the primary color for both "active" and "completed" steps. class HorizontalLinearStepper extends React.Component { state = { activeS ...

Why is it not possible to return a header in a Typescript function?

I am new to using typescript and I have encountered an issue with a value for headers. Initially, it worked fine when directly set in the code. However, when I attempted to move it into a separate function that could be called, the functionality broke. Be ...

Retrieve the template parameter from a generic type

While I have experience extracting string from string[], this particular scenario is proving to be quite challenging: type example<T = boolean> = true; // with just "example", how can I retrieve the template parameter "boolean" in this case? type T ...

What is the best way to align my header buttons in the center and move items to the far right of my toolbar?

My header currently displays as follows: https://i.sstatic.net/h8zDQ.png I am aiming to center the [LEARN MORE, ABOUT, RESOURCES, CONTACT] buttons in the middle of the navbar and align the profile icon and switch button to the far right of the page! I en ...

Why is my custom route component not displaying the React JS component?

I need assistance with implementing protected routes in my React application. When I try to access a protected route, instead of being redirected, I encounter a blank screen and the component fails to render. Why is this happening? If the following code s ...

Expanding index signature in an interface

Is there a way to redefine the old index signature when trying to extend an interface with different property types? I am encountering an error when adding new properties that have a different type from the original index signature. interface A { a: num ...

React encountered an expression when it was looking for either an assignment or function call

I'm facing an issue while trying to display the data retrieved from my database. Instead of the expected output, I am getting an error message - Failed to compile. ./src/components/list-pets.component.js Line 38:5: Expected an assignment or function ...