Error handling multiple map array in single view - javascript

I have two Select input to display data. lets say 1 Select is for product and other for display product list based on selected product in select 1. But i had an error that react didnt display my view. after i command the second Select it is run normally.
this is my Select input to select product and coverage:
<div>
<Select onChange={handleChangeSelect} value={TocId} name="ProductID" size="sm">
{TOC.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>
<Select onChange={handleChangeSelect} value={TocId} name="ProductID" size="sm">
{Coverage.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>
</div>
i get the coverage data by hit the api, after select product using hooks state
Like this:
const getCoverage = () => {
axios.post( url, data, headers).then(function (response){
if(response.data.Data == 'success') {
setCoverage(response.data.Data)
}
})
}
const handleChangeSelect = (e) => {
setTocId(e);
console.log(TocId);
getCoverage();
};

Hard to tell without seeing the rest of your code but are you fetching Coverage for the first time in handleChangeSelect?
If so, that would be the issue since Coverage doesn't exist in your first render.
A potential workaround could be to make the Coverage options render conditionally.
<div>
<Select onChange={handleChangeSelect} value={TocId} name="ProductID" size="sm">
{TOC.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>
<Select onChange={handleChangeSelect} value={TocId} name="ProductID" size="sm">
{Coverage && Coverage.length > 0 && Coverage.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>
</div>

It looks fine. As per my perception, it's an issue of empty state rendering. Render your component conditionally. Also, put a different name prop.
<div>
<Select
onChange={handleChangeSelect}
value={TocId}
name="ProductID"
size="sm"
>
{TOC?.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>
<Select
onChange={handleChangeSelect}
value={TocId}
name="ProductID"
size="sm"
>
{Coverage?.map((type, index) => (
<Select.Option key={type.id} value={type.id}>
{type.desc}
</Select.Option>
))}
</Select>

Related

I don't know where it went wrong in react js

I have created a 3 select option inputs based on 1st selection option it should populate the 2nd dropdown and based on 1st and 2nd selected option then it should populate the 3rd dropdown.
Unexpected token
const availableState = data.countries.find((c) => c.name === selectedCountry);
const availableCities = availableState?.states?.find((s) => s.name === selectedState);
render method select options
<div>
<label>Country</label>
<select
placeholder="Country"
value={selectedCountry}
onChange={(e) => setSelectedCountry(e.target.value)}
>
<option>--Choose Country--</option>
{data.countries.map((value, key) => {
return (
<option value={value.name} key={key}>
{value.name}
</option>
);
})}
</select>
</div>
<div>
<label>State</label>
<select
placeholder="State"
value={selectedState}
onChange={(e) => setSelectedState(e.target.value)}
>
<option>--Choose State--</option>
{availableState?.states.map((e, key) => {
return (
<option value={e.name} key={key}>
{e.name}
</option>
);
})}
</select>
</div>
<div>
<label>City</label>
<select
placeholder="City"
value={selectedCity}
onChange={(e) => setSelectedCity(e.target.value)}
>
<option>--Choose City--</option>
{availableCities?.cities.map((e, key) => {
return (
<option value={e.name} key={key}>
{e}
</option>
);
})}
</select>
</div>

I am working on the currency converter webapp with the fixer api

I have successfully fetched all the values. But i want to populate all of the values {currencyOptions} into the "select" option which is in the another component. How can I do that .
const [currencyOptions, setCurrencyOptions] = useState([]);
console.log(currencyOptions);
<div className="App">
<h1>Convert Currency</h1>
<CurrencyRow currencyOptions={currencyOptions} />
<SiConvertio className="convert" />
<CurrencyRow currencyOptions={currencyOptions} />
</div>
Here is the currency option component which I
const CurrencyRow = () => {
return (
//
<Container>
<Row>
<Input type="number" className="input" />
<select>
<option value="abx">abc</option>
</select>
</Row>
</Container>
);
};
You want to populate your dropdown with your currencyOptions state. Assuming your state has all the items and it's a list you want to return an option element for each item when rendering your component, something like:
currencyOptions.map((currencyOption, index) => (
<option key={index} value={currencyOption}>{currencyOption}</option>
));
Also try to use an id as the element key instead of the index if you have got one.

JSX: Conditional statement within a map

I'm trying to understand how to incorporate conditional statements within JSX. I have an array of objects that I'm mapping to a select box. I want to exclude items that include the substring "threshold" in indicator.name.
So I can't use a ternary operator, because there's no "or" item. But I can't figure out how to include an if statement within this map. Whatever I try I get an error.
<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.map(indicator => (
<option key={indicator.name} value={indicator.name}>
{indicator.label}
</option>
))}
</select>
you can filter then map:
<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.filter(indicator=>indicator.name.includes('threshold')).map(indicator => (
<option key={indicator.name} value={indicator.name}>
{indicator.label}
</option>
))}
</select>
Or return null:
<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.map(indicator => (
indicator.name.includes('threshold')?<option key={indicator.name} value={indicator.name}>:nul
{indicator.label}
</option>
))}
</select>
I would recommend using the filter method to filter out "threshold". This would return the array you are expecting to have inside JSX

Hiding Nested Data

In my react app I have I'm passing props for a view and two dropdowns. One dropdown is to let the user select an existing post to redirect to, the other is to choose a list from a 3rd party data source.
All of these are under three nested statements.
I don't want both of these dropdowns showing all the time, so I am trying to implement a simple dropdown function to hide the dropdown menu and the refresh list button:
{!this.props.view &&
<div onClick={this.toggleHidden} className="dropdown-wrapper">
{!this.state.isHidden &&
<select
className="dropdown">
<option value="none">
Redirect to an existing Post...
</option>
{this.props.sites
.filter(site => site.site !== undefined)
.map(site => (
<option value={site.site.name} key={site.id}>
{site.site.name}
</option>
))}
</select>
}
{emailProvider &&
<select className="dropdown"
>
<option key={0} value='none'>None</option>
{
emailProvider.length && emailProvider.length > 0 && emailProvider.map((eachData, key) => {
return (
<option key={key+1} value={eachData.id}>{eachData.name}</option>
)
})
}
</select>
}
<button style={{paddingTop: 14}}onClick = {this.handleClick}>Refresh List</button>
</div>
}
The hide function works for the first dropdown, but if I move the closing brace that is under the first</select> to after the </button>, the {emailProvider && code can't find the closing brace which is under the second select option. It returns an error }' expected as if the closing bracket is not there.
How do I encapsulate the two dropdowns and stop this error from occurring?
expression must have at least one parent element
{!this.props.view &&
<div onClick={this.toggleHidden} className="dropdown-wrapper">
{
!this.state.isHidden ?
<> {/*needs to be inside parent component*/}
<select
className="dropdown">
<option value="none">
Redirect to an existing Post...
</option>
{this.props.sites
.filter(site => site.site !== undefined)
.map(site => (
<option value={site.site.name} key={site.id}>
{site.site.name}
</option>
))}
</select>
<> {/*needs to be inside parent component*/}
{emailProvider &&
<select className="dropdown">
<option key={0} value='none'>None</option>
{
emailProvider.length && emailProvider.length > 0 && emailProvider.map((eachData, key) => {
return (
<option key={key + 1} value={eachData.id}>{eachData.name}</option>
)
})
}
</select>
}
</>
<button style={{ paddingTop: 14 }} onClick={this.handleClick}>Refresh List</button>
</>
:
null
}
</div>
}

how to append drop down options inside render function?

below is my code. console.log of this.Resources result is shown below
1:"Basavaraj"
2:"Ashuthosh"
3:"Sravan"
4:"Raghavendra"
5:"Prem kumar"
6:"Simran"
7:"Namratha"
8:"Ghanashri"
9:"Ravindra"
10:"Anand"
11:"Shaeen"
render() {
console.log(this.Resources);
const options = this.Resources.map((item, index) =>{
<option key={index} value={item}>{item}</option>
});
return (
<div>
<form>
<div id="select">
<div className="container select">
<div className="row">
<select className="col-4 selectpicker input-large">
<option value="" disabled selected>Select resouce</option>
{options}
</select>
I want all data in this.Resources should come in select drop down options.
Thanks in advance
You are not returning the html in map.Need to return it.
const options = this
.Resources
.map((item, index) => {
return (
<option key={index} value={item}>{item}</option>
);
});
OR
ECMA6 arrow notation with default return:
const options = this
.Resources
.map((item, index) => (
<option key={index} value={item}>{item}</option>
));

Categories

Resources