我想知道使用(或不使用)useEffect 更新 useLocation 之间的最大区别是什么。
我通常发现人们在 useEffect 中设置 useLocation 来在 URL 路径发生变化时更新状态,但我发现我不需要这样做来更新位置。
const NavBar = () => {
const location = useLocation();
const [currentPath, setCurrentPath] = useState('')
const location = useLocation();
console.log('pathname:', location.pathname)
useEffect(() => {
setCurrentPath(location.pathname);
}, [location]);
console.log('currentPath:', currentPath)
...
}
Run Code Online (Sandbox Code Playgroud)
Returns
pathname: /results:Cancer
currentPath: /results:Cancer
Run Code Online (Sandbox Code Playgroud)
我的意思是,我发现的唯一区别是,使用 useEffect 时,返回将在组件安装后发生。我正在尝试了解成为更好的程序员的最佳实践。
我正在尝试删除 .files 和 .chunk 数据,但我发现的所有帖子要么已经过时,要么不适用于我的问题。
这是我的后端路线:
const mongoose = require("mongoose");
const config = require("config");
const db = config.get("mongoURI");
let gfs;
const conn = mongoose.createConnection(db);
conn.once("open", () => {
gfs = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: "photos"
});
});
router.delete('/:imageID', async (req, res) => {
gfs.delete({_id: req.params.imageID, root:"photos"}, function(error){
test.equal(error, null);
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?