правильно ли я все понял?
componentDidUpdate(prevProps) {
const id = this.props.match.params.id;
if (id !== prevProps.match.params.id) {
this.fetchSomeData(id);
}
}
.todo-list-item.done { text-decoration: line-through; };
this.setState({ done: true });
true
назначать, а переключать текущее значение на противоположное?<span className= {classNames} onClick= {this.onLabelClick} >{ label } <button type="button" onClick= {this.onMarkClick }
class App extends React.Component {
state = {
tasks: [
{
id: 1,
title: 'title',
description: 'description',
},
],
}
createTask = () => {
this.setState(({ tasks }) => ({
tasks: [
...tasks,
{
id: 1 + Math.max(0, ...tasks.map(n => n.id)),
title: 'title',
description: 'description',
},
],
}));
}
render() {
return (
<div>
<button onClick={this.createTask}>Добавить задачу</button>
<ul>
{this.state.tasks.map(({ id, title, description }) => (
<li key={id}>id: {id} | {title} | {description}</li>
))}
</ul>
</div>
);
}
}
const Block = ({ items }) => (
<div className="info">
<ul>{items.map(n => <li>{n}</li>)}</ul>
</div>
);
class App extends React.Component {
state = {
items: [
[ 'hello, world!!', '69' ],
[ 'fuck the world', '187' ],
[ 'fuck everythign', '666' ],
],
selected: null,
}
onClick = e => {
this.setState({
selected: +e.currentTarget.dataset.index,
});
}
render() {
const { items, selected } = this.state;
return (
<div>
<div className="wrap">
{items.map((n, i) => (
<div
className={`wrap__el ${i === selected ? 'active' : ''}`}
onClick={this.onClick}
data-index={i}
>
<Block items={n} />
</div>
))}
</div>
<div className="result">
{selected !== null ? <Block items={items[selected]} /> : null}
</div>
</div>
);
}
}
не срабатывают case, всегда default
Что пофиксить, подскажите плиз.
.points {
position: relative;
height: 400px;
border: 1px solid silver;
overflow: hidden;
}
.point {
display: inline-block;
position: absolute;
width: 20px;
height: 20px;
background: red;
}
class App extends React.Component {
state = {
points: [],
}
addPoint = ({ nativeEvent: { offsetX, offsetY } }) => {
this.setState(({ points }) => ({
points: [ ...points, {
x: offsetX - 10,
y: offsetY - 10,
} ],
}));
}
render() {
return (
<div className="points" onClick={this.addPoint}>
{this.state.points.map(n => (
<div className="point" style={{
left: `${n.x}px`,
top: `${n.y}px`,
}}
></div>
))}
</div>
);
}
}
<Transactions />
class="filter-menu"
{transaction}
<div>{transaction.type}</div>
<div>{transaction.date}</div>
useEffect(() => {
const onKeypress = e => console.log(e);
document.addEventListener('keypress', onKeypress);
return () => {
document.removeEventListener('keypress', onKeypress);
};
}, []);
const Comments = ({ items }) =>
Array.isArray(items) && items.length
? <React.Fragment>{items.map(n =>
<div className="comment" key={n.id}>
<h3>{n.name}</h3>
<div>{n.body}</div>
<Comments items={n.reply} />
</div>)}
</React.Fragment>
: null;
const filteredProducts = [
[ true, n => n.type === 'Bras' ],
[ true, n => n.price <= tempPrice ],
[ color !== 'all', n => n.color === color ],
[ cup, n => n.cup.includes(cup) ],
[ shipping, n => n.freeShipping === true ],
[ search, n => n.title.toLowerCase().startsWith(search.toLowerCase()) ],
].reduce((products, [ active, fn ]) => {
return active
? products.filter(fn)
: products;
}, storeProducts);
this.setState({ filteredProducts });
event.currentTarget.getAttribute('имя_атрибута')
onClick={e => props.arrowHandler(e, VALUE)}
если условия задачи поменяются и...
<EditorWithTableOfContents tags={[ 'h1' ]} />
<EditorWithTableOfContents tags={[ 'h2', 'h3' ]} />
const EditorWithTableOfContents = ({ tags }) => {
const [headers, setHeaders] = useState([]);
const editorEl = useRef(null);
const onChange = e => {
setHeaders(Array.from(
e.editor.document.$.querySelectorAll(tags.join(', ')),
n => [ n.tagName, n.innerText ]
));
};
const goToAnchor = e => {
const index = e.target.dataset.index;
const d = editorEl.current.editor.document.$;
d.documentElement.scrollTo({
top: d.querySelectorAll(tags.join(', '))[index].offsetTop,
behavior: 'smooth',
});
};
return (
<div>
<CKEditor
data={initData}
onInstanceReady={onChange}
onChange={onChange}
ref={editorEl}
/>
<h2>Содержание</h2>
{headers.map(([Tag, text], index) => (
<Tag key={index} data-index={index} onClick={goToAnchor}>
{text}
</Tag>
))}
</div>
);
};