I have a constant.js file which has all the static arrays for the application. I need to use this file content in another component and map its value to a dropdown.
1 Answer
You can do like this:-
constant.js
const array = [
{
id:1, value:'option 1'
},
{
id:2, value:'option 2'
},
{
id:3, value:'option 3'
},
{
id:4, value:'option 5'
},
{
id:5, value:'option 5'
}
]
export default array
Now import this file to your component and use it
import React, { Component } from "react";
import Array from './constant'
export default class YourComponent extends Component{
render(){
return(
<div>
<select>
{
Array.map((item, i) =>{
return(
<option key={i}>
{item.value}
</option>
)
}
}
</select>
</div>
)
}
}
2 Comments
Devika Raj
But now again have a problem , if suppose I have another dropdown also . The 2nd drop down should be populated on selection of any value in first dropdown . How can i achieve it? Please help anyone.
Vikas Singh
for that you can follow this answer stackoverflow.com/questions/41916852/…
importthearray constantin yourcomponent.