function Pokemon({ name, url }) {
const [ data, setData ] = useState(null);
useEffect(() => {
fetch(url).then(r => r.json()).then(setData);
}, [ url ]);
return (
<div>
<h4>{name}</h4>
{data
? <div>
<ul>{data.abilities.map(n => <li>{n.ability.name}</li>)}</ul>
<img src={data.sprites.front_default} />
</div>
: <div>loading...</div>
}
</div>
);
}
const lines = data.split('\n').map(n => n.replace(/X\S+/, 'hello, world!!'));
document.body.addEventListener('click', e => {
document.querySelectorAll('details[open]').forEach(n => {
n.open = n === e.target.parentNode;
});
});
index = next((i for i, n in enumerate(arr) if key in n), -1)
value = next((n[key] for n in arr if key in n), None)
data: () => ({
blockTypes: [ 'block-item1', 'block-item2' ],
blocks: [],
}),
<button v-for="n in blockTypes" @click="blocks.push(n)">add {{ n }}</button>
<component v-for="n in blocks" :is="n"></component>
return this.arr.sort(...
Метод sort()
на месте сортирует элементы массива
копия массива не создаётся
methods: {
trClass: rowData => rowData.is_blocked && 'table-danger',
...
<b-table
:tbody-tr-class="trClass"
...
const Filter = ({ title, values, value, onChange }) => (
<div>
<h3>{title}</h3>
{values.map(n => (
<label>
<input
type="radio"
onChange={() => onChange(n)}
checked={value === n}
/>
{n}
</label>
))}
</div>
);
state = {
...
filteredProducts: [],
status: 'all',
}
onFilterStatusChange = status => {
this.setState({ status });
}
filterProducts() {
this.setState(({ status }) => ({
filteredProducts: status === 'all'
? this.state.products
: this.state.products.filter(n => n.prod_status.includes(status)),
}));
}
componentDidUpdate(prevProps, prevState) {
if (this.state.status !== prevState.status) {
this.filterProducts();
}
}
render() {
...
<Filter
title="Status:"
values={[ 'all', 'recommended', 'saleout', 'bestseller', 'new' ]}
value={this.state.status}
onChange={this.onFilterStatusChange}
/>
<Products products={this.state.filteredProducts} />
...
}
<div className="status">{status}</div>
пусть будет{status.split(',').map(n => <div className="status">{n}</div>)}
function getDividers(num, divider) {
return num === 1
? []
: num % divider
? getDividers(num, divider + 1)
: [ divider, ...getDividers(num / divider, divider) ];
}
function showDividers(num) {
if (!Number.isInteger(num) || num < 2) {
throw 'fuck off';
}
console.log(`${num} = ${getDividers(num, 2).join(' * ')}`);
}
function createTree({
data,
idKey = 'id',
parentKey = 'parentId',
childrenKey = 'children',
}) {
const tree = Object.fromEntries(data.map(n => [ n[idKey], { ...n, [childrenKey]: [] } ]));
return Object.values(tree).filter(n => !tree[n[parentKey]]?.[childrenKey].push(n));
}
const result = createTree({
data: components,
childrenKey: 'components',
});
data: () => ({
numArticlesToShow: 5,
...
}),
computed: {
articlesToShow() {
return this.articles.slice(0, this.numArticlesToShow);
},
...
},
<li v-for="n in articlesToShow">
...
</li>
...
<button @click="numArticlesToShow += 5" :disabled="numArticlesToShow >= articles.length">
показать ещё
</button>
this.$set(this.chartOptions.series[0].data, индекс, значение);