Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Admin dynamic url based on filters in return

I am quite beginners in coding. I have one question, hope you could help me !
I have a little function which returns a url and I try to make it dynamic, based on filters. I need to make my return dynamic, it means if one oof my filter is unset(so undefined), ex: q=undefined or size=undefined, so in my url I want to delete this part completely : "q":"${q}". my problem is this part of my url : `…"q":"${q}", "size":"${size}", "color":"${color}"}

 rowClick={(id, resource, record) => {
        setSelectedRecord(record.product)
        const { q, size, color } = filterValues
        return `?filter={"details":"${record.details}","type":"product", "q":"${q}",  "size":"${size}", "color":"${color}"}`
      }}

I want my url in return was dynamic :/ no idea how to make it 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Well you could use some ternary operators and do as:

`?filter={${record.details ? `"details":${record.details}",`}"type":"product",${q ? `"q":${q}",`} ${size ? `"size":${size}",`}${color ? `"color":${color}",`}`

I am not sure you need all those double quotes, since it’s all a string anyway so you should try not using them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading