【发布时间】:2020-11-10 08:49:25
【问题描述】:
当用户在页面“/items/:itemId”中使用 react 和 typescript 时,我想将右属性从 16px 更改为 40px。
下面是我的组件sn-p,
const root = () => {
<PopupContextProvider>
<App/>
</PopupContextProvider>
}
export const PopupContextProvider = ({ children }: any) => {
return (
<popupContext.Provider value={context}>
{children}
{(condition1 || condition2) && (
<Popup onHide={dismiss} />
)}
</popupContext.Provider>
);
}
export function Popup({ onHide }: Props) {
const location = useLocation();
const [isView, setIsView] = React.useState(false);
if (location.pathname === '/items/:itemId') {
setIsView(true);//here doesnt change to true. how can i do the same
//in useeffect or something that updates
}
return (
<Dialog isView={isView}>
<DialogBody>
<span>Title</span>
<Description/>
</DialogBody>
<Actions>
<span>Hide</span>
</Actions>
</Dialog>
);
}
const Dialog = styled.div<isView?:boolean>`
position: fixed;
${({ isView }) => isView && 'right: 40px;'}
display: flex;
flex-direction: column;
`;
在上面的 sn-p 中,我检查位置并将 isView 状态更新为 true。
现在即使用户在页面“/items/:itemId”中,isView 也不会从 false 更新为 true。
如何更新使用效果中的状态???
有人可以帮我解决这个问题吗?谢谢。
【问题讨论】:
-
这个问题已经被问过了。前往link
标签: reactjs typescript