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.
Asked
Active
Viewed 9,060 times
0
-
8Welcome in Stackoverflow. We’d love to help you. To get a good Answer for your Question: Can you provide some code and/or more explanations of what you are doing, and what's wrong if there is. Please read https://stackoverflow.com/help/how-to-ask – Shim-Sao Dec 23 '18 at 17:01
-
Possible duplicate of [How to create helper file full of functions in react native?](https://stackoverflow.com/questions/38402025/how-to-create-helper-file-full-of-functions-in-react-native) – c-chavez Dec 24 '18 at 08:20
-
Why not to simply `import` the `array constant` in your `component`. – Nagama Inamdar Dec 24 '18 at 12:02
1 Answers
8
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>
)
}
}

Vikas Singh
- 1,791
- 1
- 14
- 26
-
1But 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. – Devika Raj Dec 24 '18 at 09:44
-
1for that you can follow this answer https://stackoverflow.com/questions/41916852/react-js-populate-2nd-dropdown-based-on-selection-from-1st-dropdown – Vikas Singh Dec 24 '18 at 09:57