const validator = (value, maxSize) =>
!(/[^а-яА-Яa-zA-Z0-9\s]/.test(value) || value.length > maxSize);
if (validator(value, maxSize)) {
setTitle(value);
}
const validator = (defaultValue, value, maxSize) =>
(/[^а-яА-Яa-zA-Z0-9\s]/.test(value) || value.length > maxSize)
? defaultValue
: value;
setTitle(prevValue => validator(prevValue, value, maxSize));
// ...прилегают к первой кнопке
const showAfterFirst = currentPage < 5;
// ...прилегают к последней кнопке
const showBeforeLast = currentPage > totalPages - 4;
!showAfterFirst
и !showBeforeLast
. А чтобы отобразить центральную группу кнопок, оба базовых условия должны быть ложными: !(showAfterFirst || showBeforeLast)
..container {
display: grid;
grid-template-columns: repeat(5, 1fr);
}
function App() {
const [ count, setCount ] = useState(0);
const onChange = e => setCount(Math.max(0, e.target.value | 0));
return (
<div>
<input type="number" value={count} onChange={onChange} />
<div className="container">
{Array.from(Array(count).keys(), n => <div className="item">{n}</div>)}
</div>
</div>
);
}
const rootEl = useRef(null);
useEffect(() => {
const onClick = e => rootEl.current.contains(e.target) || console.log('клик вне компонента');
document.addEventListener('click', onClick);
return () => document.removeEventListener('click', onClick);
}, []);
return (
<div ref={rootEl}>
...
</div>
);
nextSlide = ({ target: { dataset: { step } } }) => {
this.setState(({ images: { length }, currentImageIndex: i }) => ({
currentImageIndex: Math.max(0, Math.min(length - this.props.slidesToShow, i + +step)),
}));
};
<button onClick={this.nextSlide} data-step="-1">PREV</button>
<button onClick={this.nextSlide} data-step="+2">NEXT</button>
function App(props) {
const [ items, setItems ] = React.useState([...props.items]);
const [ name, setName ] = React.useState('');
const add = () => setItems([
...items,
{
id: 1 + Math.max(0, ...items.map(n => n.id)),
name,
},
]);
return (
<div>
<div>
<input onChange={e => setName(e.target.value)} />
<button onClick={add}>add</button>
</div>
{items.map(n => (
<div key={n.id}>
{Object.entries(n).map(([ k, v ]) => <div>{k}: {v}</div>)}
</div>
))}
</div>
);
}
const types = [ 'text', 'password', 'number' ];
const [ type, setType ] = useState('text');
<input type={type} />
const onTypeChange = e => setType(e.target.value);
{types.map(n => (
<input
type="button"
value={n}
className={n === type ? 'active' : ''}
onClick={onTypeChange}
/>
))}
{types.map(n => (
<label>
<input
type="radio"
value={n}
checked={n === type}
onChange={onTypeChange}
/>
{n}
</label>
))}
<select value={type} onChange={onTypeChange}>
{types.map(n => <option>{n}</option>)}
</select>
<button onClick={() => setType(types[(types.indexOf(type) + 1) % types.length])}>
next type
</button>
const uniqueWithCount = (arr, idKey, countKey) =>
Object.values(arr.reduce((acc, n) => (
(acc[n[idKey]] ??= { ...n, [countKey]: 0 })[countKey]++,
acc
), {}));
const result = uniqueWithCount(arr, 'name', 'qty');
const addToCart = product =>
setCart(cart => cart.some(n => n.id === product.id)
? cart.map(n => n.id === product.id ? { ...n, qty: n.qty + 1 } : n)
: [ ...cart, { ...product, qty: 1 } ]
);
taskList.map(function(item) { return ( <Task data_text={item.text} key={item.id} delete={this.deleteTaskHandler}/> ) })
this.deleteTaskHandler
- это не метод компонента.onCLick={this.props.deleteTaskHandler()}
deleteTaskHandler
, если передаёте в экземпляр компонента delete
?function App() {
const [ numbers, setNumbers ] = React.useState([ 1, 2, 3 ]);
const [ timerId, setTimerId ] = React.useState(null);
const add = () => setNumbers(numbers => [ ...numbers, Math.random() * 10 | 0 ]);
const start = () => setTimerId(setInterval(add, 1000));
const stop = () => (clearInterval(timerId), setTimerId(null));
return (
<div>
<ul>
{numbers.map(n => <li>{n}</li>)}
</ul>
<div>
<button onClick={add} disabled={timerId}>Добавить число</button>
<button onClick={start} disabled={timerId}>Старт</button>
<button onClick={stop} disabled={!timerId}>Стоп</button>
</div>
</div>
);
}
const arr = useMemo(() => {
const multiple = [ 'Format', 'Stars' ];
return str
.split('\n\n')
.map(n => n
.split('\n')
.reduce((acc, m) => (
m = m.split(/(?<=^[^:]+): /),
acc[m[0]] = multiple.includes(m[0]) ? m[1].split(', ') : m[1],
acc
), {}));
}, [ str ]);
<ul>
{arr.map(n => (
<li>
<h3>{n.Title}</h3>
<div>Year: {n['Release Year']}</div>
<div>Format: <ul>{n.Format.map(m => <li>{m}</li>)}</ul></div>
<div>Stars: <ul>{n.Stars.map(m => <li>{m}</li>)}</ul></div>
</li>
))}
</ul>
onMarkerMouseOver() {
this.openPopup();
}
<Marker onMouseOver={this.onMarkerMouseOver}>
<Input handleInput={this.handleInput} />
onChange={this.props.handleInput}