我正在制作一个 React 应用程序,我只需要Javascript
在一个组件中运行一个脚本,引用其中的一个div
。
我知道有一些图书馆可以这样做,但我想知道是否有更直接的方法来做到这一点。现在我这样做:
import React, { Component } from "react";
import "../ThisComponent.css";
export default class MyComponent extends Component {
render() {
return (
<div>
<div id="scriptTarget" />
<div className="main">
// Other stuff
</div>
<script src="js/myScript.js" />
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
但什么也没有发生。脚本存储在public/js/myScript.js
.
谢谢!
I want to know if the user has scrolled or not to update the UI in NextJS. I have the following code, all the examples I have found have the same code:
const [scrollY, setScrollY] = useState(0);
const onScroll = (event) => {
const { pageYOffset, scrollY } = window;
console.log("yOffset", pageYOffset, "scrollY", scrollY);
setScrollY(window.pageYOffset);
};
useEffect(() => {
document.body.addEventListener("scroll", onScroll, { passive: true });
// remove event on unmount to prevent a memory leak
() => document.removeEventListener("scroll", onScroll, { …
Run Code Online (Sandbox Code Playgroud) 我是 Firestore 的新手,我正在尝试将 MongoDB 项目迁移到 Firestore。我有以下问题。我的数据结构是这样的:
/products
'A': {name: 'Product A', price: 1},
'B': {name: 'Product B', price: 2}
/profiles
'1': {name: 'John', purchases: [ {product: 'A', quantity: 1} ] }
Run Code Online (Sandbox Code Playgroud)
我有一系列产品和一系列配置文件。这些配置文件有一个购买数组,每个购买都有一个对相应产品和数量的对象引用。使用 MongoDb,我可以这样做:
/products
'A': {name: 'Product A', price: 1},
'B': {name: 'Product B', price: 2}
/profiles
'1': {name: 'John', purchases: [ {product: 'A', quantity: 1} ] }
Run Code Online (Sandbox Code Playgroud)
所以我可以得到以下对象:
{
_id: '1',
name: 'John',
purchases: [
{
{
_id: 'A',
name: 'Product A',
price: 1,
},
quantity: 1,
}
] …
Run Code Online (Sandbox Code Playgroud)