Reactjs Dynamic Drop Down Populated With Map Function

reactJS dynamic drop down populated with map function

obj is an object, it doesn't have a map function. Whereas obj.array is a list that have a map function.

The following code

let optionItems = obj.map((item) =>
<option key={item.array}>{item.array}</option>
);

should be

let optionItems = obj.array.map((item) =>
<option key={item}>{item}</option>
);

Open dropdown at a time in reactjs from a map function

Create a dropdown component that you can use everywhere and have whatever is including it open or close it.

const Dropdown = ({items, selected, open}) => {
return <div className='dropdown'>
// use items and selected here to create dropdown
</div>
}

Now you can import that everywhere and just pass down the items, selected item and whether or not it's open from whatever component is using it.

Steps to populate dynamic Dropdown using arrays in REACTJS using react hooks

You can abstract the country in a separate list with the "Object.keys" and use the first one to get the cities. I think in this case you don't need to use "useEffect".

Example here: https://codesandbox.io/s/dropdown-react-p0nj7

import React, { useState } from "react";
import "./styles.css";

export default function App() {
const [cities, setCities] = useState([]);
const [selectedCounty, setSelectedCountry] = useState("");
const [selectedCity, setSelectedCity] = useState("");

const countries = {
France: ["Paris", "Marseille", "Lille", "Lyon"],
Usa: ["New York", "San Francisco", "Austin", "Dallas"],
Brazil: ["São Paulo", "Rio de Janeiro", "Salvador"]
};

const countryList = Object.keys(countries).map(key => ({
name: key
}));

function handleCountrySelect(e) {
console.log("Selected country", e.target.value);
const countrySel = e.target.value;
const citiesSel = countrySel !== "" ? countries[countrySel] : [];
setSelectedCountry(countrySel);
setCities(citiesSel);
setSelectedCity("");
}

function handleCitySelect(e) {
console.log("Selected city", e.target.value);
const citiesSel = e.target.value;
setSelectedCity(citiesSel);
}

return (
<div className="App">
<h1>Example DropDown Coutries and Cities</h1>

<div className="Container">
<select
name="Countries"
onChange={e => handleCountrySelect(e)}
value={selectedCounty}
>
<option value="">Select the country</option>
{countryList.map((country, key) => (
<option key={key} value={country.name}>
{country.name}
</option>
))}
</select>

<select
name="Cities"
onChange={e => handleCitySelect(e)}
value={selectedCity}
>
<option value="">Select the city</option>
{cities.map((city, key) => (
<option key={key} value={city}>
{city}
</option>
))}
</select>
</div>
</div>
);
}

How do I create a dynamic drop down list with react-bootstrap

You can start with these two functions. The first will create your select options dynamically based on the props passed to the page. If they are mapped to the state then the select will recreate itself.

 createSelectItems() {
let items = [];
for (let i = 0; i <= this.props.maxValue; i++) {
items.push(<option key={i} value={i}>{i}</option>);
//here I will be creating my options dynamically based on
//what props are currently passed to the parent component
}
return items;
}

onDropdownSelected(e) {
console.log("THE VAL", e.target.value);
//here you will see the current selected value of the select input
}

Then you will have this block of code inside render. You will pass a function reference to the onChange prop and everytime onChange is called the selected object will bind with that function automatically. And instead of manually writing your options you will just call the createSelectItems() function which will build and return your options based on some constraints (which can change).

  <Input type="select" onChange={this.onDropdownSelected} label="Multiple Select" multiple>
{this.createSelectItems()}
</Input>

Issues populating dynamic dropdown with React Hooks

Currently the function passed to .map() doesn't return anything. You can fix this by replacing the curly braces with parentheses:

{users.map((i) => (
<option value={i.name}>{i.name}</option>
))}

When curly braces are used in an arrow function you need an explicit return statement to return a value. Without curly braces there is an implicit return on the one line of the function body. (In this case parentheses just help define that one line since it includes carriage returns.)



Related Topics



Leave a reply



Submit