this.setState(state => Object
.keys(state)
.reduce((acc, n) => (acc[n] = n === 'свойствоКотороеДолжноБытьTrue', acc), {})
);
this.setState({ input: this.state.output.toString() }); this.showAnswer();
Think of setState()
as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.
this.setState({
input: this.state.output.toString()
}, this.showAnswer);
бибилиотека react-yandex-maps не имеет такой функциональности
return { actionList };
<Range steps={[ 500, 1000, 5000, 10000 ]} />
class Range extends React.Component {
state = {
value: this.props.steps[0],
}
onChange = ({ target: { value } }) => {
this.setState({
value: this.props.steps.reduce((closest, n) =>
Math.abs(closest - value) < Math.abs(n - value) ? closest : n
),
});
}
render() {
const { steps } = this.props;
return (
<div>
<input
type="range"
min={steps[0]}
max={steps[steps.length - 1]}
value={this.state.value}
onChange={this.onChange}
/>
<p>{this.state.value}</p>
</div>
);
}
}
text={<a href="https://toster.ru">toster</a>}
class App extends React.Component {
state = {
maxCount: 55,
people: [],
url: 'https://swapi.dev/api/people/',
}
fetchPeople() {
fetch(this.state.url).then(r => r.json()).then(r => {
this.setState(({ people, maxCount }) => ({
people: [ ...people, ...r.results ].slice(0, maxCount),
url: r.next,
}));
});
}
updateGenderCount() {
this.setState(({ people }) => ({
genderCount: people.reduce((acc, n) => {
acc[n.gender] = (acc[n.gender] || 0) + 1;
return acc;
}, {}),
}));
}
componentDidMount() {
this.fetchPeople();
}
componentDidUpdate(prevProps, prevState) {
const count = this.state.people.length;
if (count !== prevState.people.length) {
this.updateGenderCount();
if (this.state.url && count < this.state.maxCount) {
this.fetchPeople();
}
}
}
render() {
return (
<div>
<pre>{JSON.stringify(this.state.genderCount, null, 2)}</pre>
</div>
);
}
}
class App extends React.Component {
state = {
items: [...Array(20).keys()],
chunkLen: 3,
}
onClick = ({ target: { dataset: { change } } }) => {
this.setState(({ chunkLen }) => ({
chunkLen: Math.max(2, Math.min(9, chunkLen + +change)),
}));
}
render() {
const { items, chunkLen } = this.state;
const chunks = Array.from(
{ length: Math.ceil(items.length / chunkLen) },
(n, i) => items.slice(i * chunkLen, (i + 1) * chunkLen)
);
return (
<div>
<button data-change="-1" onClick={this.onClick}>-</button>
{chunkLen}
<button data-change="+1" onClick={this.onClick}>+</button>
<div>
{chunks.map(chunk => (
<div className="group">
{chunk.map(n => <div className="group-item">{n}</div>)}
</div>
))}
</div>
</div>
);
}
}
class App extends React.Component {
state = {
items: Array.from({ length: 10 }, (n, i) => ({
id: i + 1,
value: String.fromCharCode(97 + i).repeat(5),
})),
active: [],
}
toggleActive = e => {
const id = +e.target.dataset.id;
this.setState(({ active }) => ({
active: active.includes(id)
? active.filter(n => n !== id)
: [ ...active, id ],
}));
}
render() {
const { items, active } = this.state;
return (
<ul>
{items.map(n => (
<li
key={n.id}
data-id={n.id}
onClick={this.toggleActive}
className={active.includes(n.id) ? 'active' : ''}
>
{n.value}
</li>
))}
</ul>
);
}
}
const classes = {
0: 'first',
[props.data.length - 1]: 'last',
};
className={classes[i]}
.