DEV Community

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. جلب الحبيب عن طريق اسمه بالقران