【问题标题】:How to update state in useeffect based on url using react and typescript?如何使用react和typescript根据url更新useeffect中的状态?
【发布时间】: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


【解决方案1】:

您根本不需要 isView 状态,只需将其设为 const。

export function Popup({ onHide }: Props) {
    const location = useLocation();
    const isView = location.pathname === '/items/:itemId';
    return (
        <Dialog isView={isView}>
            <DialogBody>
                <span>Title</span>
                <Description/>
            </DialogBody>
            <Actions>
                <span>Hide</span>
            </Actions>
        </Dialog>
    );
}

只有当 React 重新呈现组件时,您需要记住发生变化的事情时才需要状态,例如用户输入信息或单击按钮。但是,在这种情况下,您可能不会在不重新加载页面并获取组件的新实例的情况下更改路径。

如果你真的想使用useEffect,你可以把一个空数组作为第二个参数,这意味着它只会在创建组件时运行,就像类 React 中的componentDidMount。

export function Popup({ onHide }: Props) {
    const location = useLocation();
    const [isView, setIsView] = React.useState(false);
    useEffect(() => {
        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>
    );
}

您可能会收到一条警告,提示您需要将isView 添加到最后的空数组中。 React 希望您观察正在修改的值,并在它们更改时调用 useEffect。你可以添加它,但它很奇怪,因为它永远不会改变。

您可以将 isView 存储在 ref 中,然后您不会收到警告,因为 ref 更新不会导致重新渲染,但同样没有理由为重新渲染存储此变量,它可以快速轻松地计算.

【讨论】:

  • 谢谢,仍然可以将 location.pathname 用于动态值,例如此路径“/items/:itemId”,这里 itemId 可以是任何数字。
  • 这是第一次在单击该页面中的按钮时,弹出窗口安装在页面“/items”中。现在,当用户导航到“/items/:itemId”时,我想检查这个 url。出于某种原因,isView 也没有更新使用效果。请你帮助我好吗。我从现在开始尝试这个。
  • 你的意思是当位置改变时你想改变isView?这应该发生在useLocation 电话中。
猜你喜欢
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 2020-11-08
  • 2020-08-16
  • 1970-01-01
  • 2020-03-01
  • 2022-01-04
  • 2020-06-05
相关资源
最近更新 更多