我正在使用 NextJS 图像组件。我希望图像填充 100% 宽度并采用保持纵横比所需的高度。我已经尝试了此代码的许多不同变体,但除非我在父 div 上定义固定高度,否则它将无法工作。
export default function PhotoCard({ img }: Props) {
return (
<div className="relative w-full h-32 my-2">
<Image alt={img.title} src={img.url} layout="fill" objectFit="cover" />
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
我有一个关于这个问题的解决方案的问题。它有效,但我的问题是为什么我必须写
stones = stones.slice(0 ,length - 2)
Run Code Online (Sandbox Code Playgroud)
代替
stones.slice(0 ,length - 2)
Run Code Online (Sandbox Code Playgroud)
像 sort() 和 push 没有它的工作。
var lastStoneWeight = function (stones) {
stones.sort((a, b) => a - b);
let length = stones.length
if(length <= 1) return stones
let replace = stones[length - 1] - stones[length - 2]
stones = stones.slice(0 ,length - 2) // <----- HERE
stones.push(replace)
if (stones.length > 1) return lastStoneWeight(stones)
return stones
};
console.log(lastStoneWeight([8,10,4])); //return [2]
Run Code Online (Sandbox Code Playgroud) 我正在使用一个函数 (getActorInfo()) 来响应从 api 获取信息并将其设置为状态。它可以工作,但该功能不会停止运行。
export default function ActorProfile({ name, img, list, id, getActorInfo }) {
const [showList, setShowList] = useState(false);
const [actorInfo, setActorInfo] = useState({});
getActorInfo(id).then(val => setActorInfo(val));
console.log(actorInfo)
return (
<Wrapper>
<Actor
id={id}
name={name}
img={img}
onClick={() => {
setShowList(!showList);
}}
actorBirthday={actorInfo.actorBirthday}
/>
{showList && <MovieList list={list} actorInfo={actorInfo} />}
</Wrapper>
);
}
Run Code Online (Sandbox Code Playgroud)
我试过像这样使用 useEffect
useEffect(() => {
getActorInfo(id).then(val => setActorInfo(val));
}, {});
Run Code Online (Sandbox Code Playgroud)
但我收到一个我不明白的错误
无法对卸载的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 useEffect 清理函数中的所有订阅和异步任务。在 ActorProfile 中(在 App.js:60)
我的问题是如何让这个函数只运行一次?
我在解决这个承诺时遇到了麻烦。
const getAge = (actorId) => {
return (
axios.get(`https://api.themoviedb.org/3/person/${actorId}?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`)
.then(function (response) {
return moment().diff(response.data.birthday, 'years')
})
.catch(function (error) {
console.log(error);
})
)
}
console.log(getAge('5000'), 'FUNCTION')
Run Code Online (Sandbox Code Playgroud)
它永远不会解决承诺并返回待定
我尝试了许多不同的想法,但似乎都没有奏效。我错过了什么?
谢谢
reactjs ×3
javascript ×2
jsx ×2
arrays ×1
next.js ×1
nextjs-image ×1
promise ×1
state ×1
tailwind-css ×1
use-effect ×1