DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Dennis kinuthia
Dennis kinuthia

Posted on

Custom react icon context wrapper

React icons are cool and all but tey do have that pesky issue of hvving to wrap them in an in order to be able to resize them or change their colors so i made this wrapper

import React from 'react'
import { IconContext, IconType } from "react-icons";

type MyProps = {
  // using `interface` is also ok
  Icon: IconType;
  size: string;
  color: string;
  iconstyle?:string;
  iconAction?: () => any;
};
type MyState = {
  iconstyle: string;
};
export class TheIcon extends React.Component<MyProps, MyState> {
   constructor(props:MyProps) {
    super(props)
    this.state = { iconstyle:this.props?.iconstyle?this.props?.iconstyle:"" };
    this.clickAction = this.clickAction.bind(this); 
    }
    clickAction(){
      if(this.props.iconAction){
      console.log("click action")
      return this.props.iconAction()
      }
      return console.log("")
      }
     render() {
    return (

      <div>
        <IconContext.Provider value={{ size:this.props.size,color:this.props.color,
          className:this.state.iconstyle}}>
            <this.props.Icon onClick={()=>this.clickAction()}/>
        </IconContext.Provider>

      </div>
    );
  }
}


Enter fullscreen mode Exit fullscreen mode

example usage

import { FaTimes} from "react-icons/fa";
import { TheIcon } from './../Shared/TheIcon';

<TheIcon Icon={ FaTimes } size={"34"} color={"green"} />

Enter fullscreen mode Exit fullscreen mode

full project
live demo

Top comments (1)

Collapse
 
aabirahaasmaa profile image
AabirahAasmaa

Luckily, the React Icon library allows us to customize the size and color of these icons. Color. To do so, we will need to use React's Context. ุฌู„ุจ ุงู„ุญุจูŠุจ ุนู† ุทุฑูŠู‚ ุงุณู…ู‡ ุจุงู„ู‚ุฑุงู†

๐Ÿ‘‹ Hey, my name is Noah and Iโ€™m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, Iโ€™d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: