<select>
{this.state.currencies.map((currencies) =>(
<option>
<div className={s.option} >{currencies.symbol} {currencies.label}
</div>
</option>
)
)
}
</select>
const Currency_Query = gql`
{
currencies{
label
symbol
}
}
`
;
class Currency extends Component {
constructor(props){
super(props);
this.state = {
currencies: ["$ USD", "€ EURO"],
}
}
componentDidMount = async () =>{
const response = await client.query({
query: Currency_Query
})
this.setState({
currencies:response.data.currencies
})
}
render() {
return(
<div className={s.select}>
<select>
{this.state.currencies.map((currencies) =>(
<option>
<div className={s.option} >{currencies.symbol} {currencies.label}
</div>
</option>
)
)
}
</select>
</div>
);
}
}
componentDidMount(){
this.Data = JSON.parse(localStorage.getItem('data'));
if(localStorage.getItem('data')){
this.setState({
selectValue: this.Data.selectValue,
countId: this.Data.countId,
})
} else {
this.setState({
selectValue: "$ USD",
countId: {quantityId:[]}
})
}
}
componentWillUpdate(nextProps, nextState){
localStorage.setItem('data', JSON.stringify(nextState))
}
<select name="nm" id="nam">
<option value="QBC">QBC</option>
<option value="QEF">QEF</option>
<option value="GHI" selected>GHI</option> <!-- этот вариант будет выбран-->
<option value="KLM">KLM</option>
</select>
import React, { useState, useEffect } from 'react'
const LocalStorage = () => {
const date = [
{ symbol: 'RU', label: 'рубль' },
{ symbol: 'USD', label: 'доллар' },
{ symbol: 'EUR', label: 'евро' }
]
const [curr, setCurrencies] = useState(date)
const [currentSymbol, setCurrentSymbol] = useState("EUR") // опция выбранная по умолчанию (если нет данных в localstorage)
// срабатывает один раз при загрузке компонента
useEffect(() => {
if(localStorage.getItem('symbol')) {
const item = localStorage.getItem('symbol')
setCurrentSymbol(item)
}
}, [])
// обработчик - срабатывает при выборе опции списка
const selectHandler = (e) => {
localStorage.setItem('symbol', e.target.value)
setCurrentSymbol(e.target.value)
}
return (
<div>
<select onChange={selectHandler} value={currentSymbol}>
{
curr.map(currencies => (
<option key={currencies.symbol} value={currencies.symbol}>
{currencies.symbol} {currencies.label}
</option>
)
)
}
</select>
</div>
)
}
export default LocalStorage
import React, { Component } from 'react'
class ClassLocal extends Component {
constructor(props){
super(props);
this.state = {
curr: [
{ symbol: "RU", label: "рубль" },
{ symbol: "USD", label: "доллар" },
{ symbol: "EUR", label: "евро" }
],
currentSymbol: "EUR"
}
}
componentDidMount() {
if(localStorage.getItem('symbol')) {
const item = localStorage.getItem('symbol');
this.setState({currentSymbol: item})
}
}
selectHandler = (e) => {
localStorage.setItem('symbol', e.target.value)
this.setState({currentSymbol: e.target.value})
}
render() {
return (
<div>
<select onChange={this.selectHandler} value={this.state.currentSymbol}>
{
this.state.curr.map(currencies => (
<option key={currencies.symbol} value={currencies.symbol}>
{currencies.symbol} {currencies.label}
</option>
)
)
}
</select>
</div>
)
}
}
export default ClassLocal