I have come across two rather simple solutions that seem to do the job, although I can't guarantee they are the most efficient.
If you have any other suggestions or improvements, please feel free to share them. If there is a superior solution, I will give it the recognition it deserves :)
Solution 1
One approach is to use the SVG file as the source for an
<button className="search__button">
<img className="search__icon" src="/img/SVG/magnifying-glass.svg" />
</button>
This method stood out to me as the most effective one at first, but I encountered some issues with it initially. In my React project utilizing bundle.js and webpack, the path ended up being different than expected. So if you opt for this solution and encounter a broken image, try experimenting with the path. I went through several variations of the path, including starting with ../
, and eventually, found a path that worked.
Solution 2
Alternatively, you can directly insert the SVG code into your JSX code. It may not be the most elegant solution, but it gets the job done.
<button className="search__button">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20">
<title>magnifying-glass</title>
<path d="M17.545 15.467l-3.779-3.779c0.57-0.935 0.898-2.035 0.898-3.21 0-3.417-2.961-6.377-6.378-6.377s-6.186 2.769-6.186 6.186c0 3.416 2.961 6.377 6.377 6.377 1.137 0 2.2-0.309 3.115-0.844l3.799 3.801c0.372 0.371 0.975 0.371 1.346 0l0.943-0.943c0.371-0.371 0.236-0.84-0.135-1.211zM4.004 8.287c0-2.366 1.917-4.283 4.282-4.283s4.474 2.107 4.474 4.474c0 2.365-1.918 4.283-4.283 4.283s-4.473-2.109-4.473-4.474z"></path>
</svg>
</button>