0

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.

3
  • 8
    Welcome 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 stackoverflow.com/help/how-to-ask Commented Dec 23, 2018 at 17:01
  • Possible duplicate of How to create helper file full of functions in react native? Commented Dec 24, 2018 at 8:20
  • Why not to simply import the array constant in your component. Commented Dec 24, 2018 at 12:02

1 Answer 1

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>
        )
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
for that you can follow this answer stackoverflow.com/questions/41916852/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.