"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"deploy": "npm run build && gh-pages -d dist"
}
const dateA = new Date('...')
const dateB = new Date('...')
// разница в секундах с точностью до миллисекунды
const differenceInSeconds = (dateA.getTime() - dateB.getTime()) / 1000
// затем можно или отбросить дробную часть
console.log(Math.trunc(differenceInSeconds))
// или округлить одним из методов
console.log(Math.round(differenceInSeconds))
let lastReqDate
app.get('/api/v1/example', (req, res) => {
const date = new Date()
let diff
if (lastReqDate != null) {
diff = (date.getTime() - lastReqDate.getTime()) / 1000
}
if (diff != null && diff < 120) {
diff = Math.ceil(diff) // округление до ближайшего большего целого
res.header('Retry-After', diff)
res.status(429) // Too Many Requests
res.send({ retryAfter: diff })
} else {
lastReqDate = date
// выполнение запроса разрешено
}
})
if (window.matchMedia('(max-width: 700px)').matches) {
for (const linkElement of document.querySelectorAll('.order-project__more-details-btn')) {
linkElement.setAttribute('href', 'examples-foundation-640')
}
}
import { doc, onSnapshot } from 'firebase/firestore'
export default function EntitityPage({ entityId }) {
const [ entity, setEntity ] = React.useState(null)
React.useEffect(() => {
const unsubscribe = onSnapshot(doc(db, 'entities', entityId), (snapshot) => {
setEntity(snapshot.data())
})
return () => {
unsubscribe()
}
}, [entityId])
return (
<div>{JSON.stringify(entity)}</div>
)
}
function App() {
const [ seconds, setSeconds ] = React.useState(0)
// старый вариант, не работает
const startTimer = () => {
setInterval(() => {
setSeconds(seconds + 1)
}, 1000)
}
// новый вариант, работает
const startTimer = () => {
setInterval(() => {
setSeconds((seconds) => {
return seconds + 1
})
}, 1000)
}
return (
<div>
<button onClick={startTimer}>start</button>
<h1>{seconds}</h1>
</div>
)
}
class Lectures extends React.PureComponent {
state = {
lectures: null,
lecturesError: null,
lecturesPending: true,
}
async componentDidMount() {
await this.fetchLectures()
}
async fetchLectures() {
this.setState({ lectures:null, lecturesError:null, lecturesPending:true })
try {
const lectures = (await firebase.database().ref('lectures').once('value')).val()
console.log(lectures)
this.setState({ lectures, lecturesError:null, lecturesPending:false })
} catch (lecturesError) {
console.error(lecturesError)
this.setState({ lectures:null, lecturesError, lecturesPending:false })
}
}
render() {
const { lectures, lecturesError, lecturesPending } = this.state
return (
<section>
<h1>Лекции</h1>
{lecturesPending && (
<div>Загружаются...</div>
)}
{lecturesError != null && (
<div>Не удалось загрузить: {lecturesError.message}</div>
)}
{lectures != null && (
<ul>
{lectures.map((lecture) => (
<li
key={lecture.id}
>
{lecture.name}
</li>
))}
</ul>
)}
</section>
)
}
}
const authorId = 1
firebase
.database()
.ref('reviews')
.orderByChild('authorId')
.equalTo(authorId)
.once('value')
.then((snapshot) => {
})
;
class extends React.Component {
async componentDidMount() {
this.setState({
data: (await dataRef.once('value')).val()
})
}
}