state = {
block1: false,
block2: false,
}
componentDidMount() {
setTimeout(() => this.setState({ block1: true }), 3000);
setTimeout(() => this.setState({ block2: true }), 6000);
}
render() {
const { block1, block2 } = this.state;
return (
...
{block1 && <div class="block-1">Блок 1</div>}
{block2 && <div class="block-2">Блок 2</div>}
...
);
}
<div id="app"></div>
function App() {
const [ dark, setDark ] = React.useState(null);
const updateDark = e => setDark(e.type === 'mouseover' ? e.currentTarget.id : null);
return (
<React.Fragment>
{[ 'left', 'right' ].map(n => (
<div
id={n}
key={n}
onMouseOver={updateDark}
onMouseLeave={updateDark}
className={dark && dark !== n ? 'dark' : ''}
></div>
))}
</React.Fragment>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
if (novyna.lenght) {
в процессе экспериментов было установлено, что если написать в условии вообще какую-либо фигню, типа if(novyna.lenght=55), то новости выводятся как ни в чем не бывало
class News extends React.Component {
const News = ({ news }) =>
<div className="news">
{Array.isArray(news) && news.length
? news.map(n => (
<div key={n.id}>
<p className="news__author">{n.author}:</p>
<p className="news__text">{n.text}</p>
<hr/>
</div>
))
: <p>Нет</p>}
<strong>Всего новостей: {news.length}</strong>
</div>;
почему вот такой код не работает
onLoad={(inst)=>{return inst.events.add('click', clickOnMap)}}
instanceRef={inst => inst.events.add('click', clickOnMap)}
onClick={clickOnMap}
const citiesListNames = citiesList.map( item => { return item.name } )
приложение зависает от такого колличества данных
class App extends React.Component {
state = {
items: [
{ id: 69, text: 'hello, world!!' },
{ id: 187, text: 'fuck the world' },
{ id: 666, text: 'fuck everything' },
],
active: null,
}
onClick = e => {
this.setState({
active: +e.target.dataset.index,
});
}
render() {
const { items, active } = this.state;
return (
<div>
{items.map((n, i) => (
<div
key={n.id}
data-index={i}
className={i === active ? 'active' : ''}
onClick={this.onClick}
>{n.text}</div>
))}
</div>
);
}
}
const Header = ({ Top }) => (
<div className="navHeader">
<Top />
</div>
);
...
<Header Top={Top} />
class TabContent extends React.Component {
render() {
const { title, content } = this.props;
return (
<div className="tabcontent">
<h3>{title}</h3>
<p>{content}</p>
</div>
);
}
}
class Tabs extends React.Component {
state = {
active: null,
}
openTab = e => this.setState({
active: +e.target.dataset.index,
});
render() {
const { items } = this.props;
const { active } = this.state;
return (
<div>
<div className="tab">
{items.map((n, i) => (
<button
className={`tablinks ${i === active ? 'active' : ''}`}
onClick={this.openTab}
data-index={i}
>{n.title}</button>
))}
</div>
{items[active] && <TabContent {...items[active]} />}
</div>
);
}
}
const Modal = (props) => {
const onClick = e => {
if (e.target.classList.contains('close') || !e.target.closest('.modal-content')) {
props.close();
}
}
return (
<div className="modal" onClick={onClick}>
<div className="modal-content">
<span className="close">×</span>
{props.children}
</div>
</div>
);
};
const App = () => {
const [ opened, setOpened ] = React.useState(false);
const open = () => setOpened(true);
const close = () => setOpened(false);
return (
<div>
<h2>Modal Example</h2>
<button onClick={open}>Open Modal</button>
{opened && <Modal close={close}>hello, world!!</Modal>}
</div>
);
}
activeProjects.map((project, i) => { <div key={i} className="focus-projects">
render() {
const activeProjects = this.props.projects.filter(n => n.ProjectStatus === 'ACTIVE');
...
class App extends Component {
state = {
options: [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry", isDisabled: true },
{ value: "vanilla", label: "Vanilla" },
],
}
render() {
return (
<Select
options={this.state.options}
/>
);
}
}
class App extends Component {
state = {
options: [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" }
],
disabled: [ 'chocolate', 'vanilla' ],
}
isOptionDisabled = option => this.state.disabled.includes(option.value)
render() {
return (
<Select
options={this.state.options}
isOptionDisabled={this.isOptionDisabled}
/>
);
}
}
class App extends React.Component {
state = {
rates: {
RUB: 1,
},
currency: 'RUB',
currencies: [
{ name: 'RUB', label: 'RUB' },
{ name: 'USD', label: 'USD' },
{ name: 'EUR', label: 'EUR' },
],
}
onCurrencyChange = ({ target: { dataset: { currency } } }) => {
this.setState({ currency });
}
componentDidMount() {
fetch('https://www.floatrates.com/daily/rub.json')
.then(res => res.json())
.then(data => {
this.setState(({ rates }) => ({
rates: {
...rates,
...Object.values(data).reduce((acc, n) => (acc[n.code] = n.rate, acc), {}),
},
}));
});
}
render() {
const { currency, currencies, rates } = this.state;
return (
<div>
{currencies.map(({ name, label }) => (
<button
className={`btn__item ${currency === name ? 'active' : ''}`}
data-currency={name}
onClick={this.onCurrencyChange}
>
{label}
</button>
))}
{this.props.products.map(({ name, price }) => (
<div>
<span>{name}</span>
<span>{(price * rates[currency]).toFixed(2)} {currency}</span>
</div>
))}
</div>
);
}
}
const products = [
{ name: 'яблоки', price: '80' },
{ name: 'греча', price: '50' },
{ name: 'конфеты', price: '380' },
];
ReactDOM.render(<App products={products} />, document.getElementById('root'));