export default selector => document.querySelector(selector);
import React, { Component } from 'react';
import $ from './param';
import './style.css';
// ...
const Example = () => {
const [isActive, setIsActive] = useState(false);
const handleClick = () => {
setIsActive(!isActive);
};
return (
<div>
<div className={isActive ? 'active' : ''}>Example</div>
<button onClick={handleClick}>Toggle active</button>
</div>
);
};
<Script src={src} onLoad={onLoad} />
import React from 'react';
class Script extends React.Component {
static displayName = 'Script';
static scriptObservers = {};
static loadedScripts = {};
static erroredScripts = {};
static idCount = 0;
scriptLoaderId = `id${this.constructor['idCount']++}`;
componentDidMount() {
const { onError, onLoad, src } = this.props;
if (this.constructor.loadedScripts[src]) {
if (onLoad) onLoad();
return;
}
if (this.constructor.erroredScripts[src]) {
if (onError) onError();
return;
}
if (this.constructor.scriptObservers[src]) {
this.constructor.scriptObservers[src][this.scriptLoaderId] = this.props;
return;
}
this.constructor.scriptObservers[src] = { [this.scriptLoaderId]: this.props };
this.createScript();
}
createScript() {
const { onCreate, src, attributes } = this.props;
const script = document.createElement('script');
if (onCreate) {
onCreate();
}
if (attributes) {
Object.keys(attributes).forEach(prop => script.setAttribute(prop, attributes[prop]));
}
script.src = src;
if (!script.hasAttribute('async')) {
script.async = true;
}
const callObserverFuncAndRemoveObserver = (shouldRemoveObserver) => {
const observers = this.constructor.scriptObservers[src];
Object.keys(observers).forEach((key) => {
if (shouldRemoveObserver(observers[key])) {
delete observers[this.scriptLoaderId];
}
});
};
script.onload = () => {
this.constructor.loadedScripts[src] = true;
callObserverFuncAndRemoveObserver((observer) => {
if (observer.onLoad) observer.onLoad();
return true;
});
};
script.onerror = () => {
this.constructor.erroredScripts[src] = true;
callObserverFuncAndRemoveObserver((observer) => {
if (observer.onError) observer.onError();
return true;
});
};
document.body.appendChild(script);
}
componentWillUnmount() {
const { src } = this.props;
const observers = this.constructor.scriptObservers[src];
if (observers) {
delete observers[this.scriptLoaderId];
}
}
render() {
return null;
}
}
export default Script;
const { header: Header, footer: Footer } = this.props;
return (
<Fragment>
<Header />
<Footer />
</Fragment>
);
<DropDown onClickHandler={yourHandler} data={yourData} />
class Dropdown extends React.component {
render() {
const { data, onClickHandler } = this.props;
return (
<ul>
{data.map((number, i) =>
<li key={i} onClick={onClickHandler}>{number}</li>
)}
</ul>
)}
}
handleSearchChange = (e, { value }) => {
this.setState({ filter: value });
};
const results = data.filter(item => item.title.toLowerCase().includes(filter.toLowerCase()));
TransitionGroup is a state machine for managing the mounting and unmounting of components over time.
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script>
function Greetings(props) {
return React.createElement('div', null, 'Hello, World!');
}
</script>
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
import React from 'react';
При загрузке приложения, скрипты из "what" выполняются сразу, чего быть не должно.
<Button className="def_btn" size="xl" level="2" onClick={tt("dsdd")}>
<Button className="def_btn" size="xl" level="2" onClick={() => tt("dsdd")}>
handler = () => {
tt("dsdd");
};
<Button className="def_btn" size="xl" level="2" onClick={this.handler}>
Условие.
Выражение, выполняющееся на каждой интерации цикла. Если выражение истинно, цикл выполняется. Условие не является обязательным. Если его нет, условие всегда считается истиной. Если выражение ложно, выполнение переходит к первому выражению, следующему за for.
<Button size="sm" type="outline" color="danger" />
const StyledButton = styled.button`
height: ${props => props.height.sm}px;
${media.md`
height: ${props => props.height.md}px;
`}
${media.lg`
height: ${props => props.height.lg}px;
`}
`;
const getHeightProp = size => {
switch(size) {
case 'sm':
return { ... };
case 'md':
return {
sm: 32,
md: 48,
lg: 64,
};
case 'lg':
return { ... };
}
};
const Button = ({ size }) => {
return <StyledButton height={getHeightProp(size)} />;
}
GET /api/posts/
{
posts: [ ... ],
linked: {
users: { ... }.
comments: { ... },
tags: { ... },
}
}
<script>
window.__INITIAL_STATE__ = { ... };
</script>
getUsers = () => {
const { dispatch } = this.props;
fetch(`https://api.github.com/search/users?q=Donetsk`)
.then(res => res.json())
.then(res => {
return Promise.all(
res.items.map(user => fetch(`https://api.github.com/users/${user.login}`).then(res => res.json()))
).then(users => {
dispatch({ type: 'GET_USERS', users });
});
});
};
import React, { Component } from 'react';
import Child from './Child';
class Parent extends Component {
childRef = React.createRef();
render() {
return (
<Child innerRef={this.childRef} />
);
}
}
import React, { Component } from 'react';
export default class Child extends Component {
render() {
const { innerRef } = this.props;
return (
<div ref={innerRef}></div>
);
}
}