reactjsfont-awesomereact-font-awesome

How to handle the error "Could not find icon"


I create the icon name dynamically, and sometimes it happens there is no corresponding icon in the library. Is there any way to catch the error and use the default icon name?


Solution

  • You can use findIconDefinition to look up the icon name like this:

    // Set params with details of the icon you're looking up depending
    // on what variant you're using.
    let params = {
       iconName: iconName,
       prefix: prefix
       // other entries as needed
    };
    if (!findIconDefinition(params)) {
        // Use default icon here.
    }
    

    Details for findIconDefinition are at https://fontawesome.com/how-to-use/javascript-api/methods/findicondefinition