小编iam*_*ynq的帖子

如何在不重新加载页面的情况下服务器端重定向到其他页面并仍将 url 保留在 Nextjs 应用程序中?

我有一个[slug].js页面可以获取 API 以获取目标页面

export async function getServerSideProps({ query, res }) {
    const slug = query.slug;
    try {
        const destination = await RoutingAPI.matchSlug(slug);
        res.writeHead(302, { Location: destination });
        res.end();
        // return {
        //     redirect: {
        //         permanent: true,
        //         destination,
        //     },
        // }
    } catch (error) {
        return {
            notFound: true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我客户端从另一个页面重定向到 slug 页面,它会起作用并使 URL 与 slug 保持相同,但它会使浏览器重新加载。如果我使用

return {
     redirect: {
        permanent: true,
        destination,
     },
}
Run Code Online (Sandbox Code Playgroud)

它不会重新加载浏览器,但会将 URL 更改为目标,与 slug 不同。我该如何解决这个问题?我将不胜感激任何想法,谢谢

javascript reactjs server-side-rendering next.js

8
推荐指数
1
解决办法
291
查看次数

catch 中的 return 语句后代码仍然执行吗?

console.log('1');
await get()
    .then()
    .catch(() => {
        console.log('2');

        return;
    });
console.log('3');
Run Code Online (Sandbox Code Playgroud)

为什么控制台记录 1 2 3?我想在return语句之后,代码不会被执行?

javascript node.js typescript

5
推荐指数
1
解决办法
1055
查看次数

如何在 React 中的 onClick 之后测试 next/link (Nextjs) 获取页面重定向?

我想测试页面在点击 div 后被重定向,但我不知道如何,这是我的代码。非常感谢

<div className="bellTest">
   <NextLink href="/notifications">
       <Bell />
   </NextLink>
 </div>
Run Code Online (Sandbox Code Playgroud)

测试.tsx

jest.mock('next/link', () => {
     return ({ children }) => {
            return children;
     };
});

describe('Test of <HeaderContainer>', () => {
    test('Test the page redirect after click', async done => {
        const wrapper = mount( <HeaderComponent /> );
        await wrapper
            .find('.bellTest')
            .at(0)
            .simulate('click');
        // expect the page getting redirect
    });
});
Run Code Online (Sandbox Code Playgroud)

testing unit-testing reactjs enzyme next.js

5
推荐指数
1
解决办法
3707
查看次数

useImperativeHandle 和 useRef 有什么区别?

据我了解,useImperativeHandle帮助父组件能够调用其子组件的函数。您可以在下面看到一个简单的示例

const Parent = () => {
    const ref = useRef(null);
    const onClick = () => ref.current.focus();

    return <>
        <button onClick={onClick} />
        <FancyInput ref={ref} />
    </>
}

function FancyInput(props, ref) {
    const inputRef = useRef();
    useImperativeHandle(ref, () => ({
      focus: () => {
        inputRef.current.focus();
      }
    }));
    return <input ref={inputRef} />;
}

FancyInput = forwardRef(FancyInput);
Run Code Online (Sandbox Code Playgroud)

但仅使用它就可以轻松实现useRef

const Parent = () => {
    const ref = useRef({});
    const onClick = () => ref.current.focus();

    return <>
        <button onClick={onClick} />
        <FancyInput …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hooks

5
推荐指数
1
解决办法
594
查看次数

删除使用 Material ui 和 Reactjs 创建的列表中的任何项目

一旦用户单击该特定项目,我就尝试删除该列表项目。

我创建了一个函数来进行样式更改

const completed = () =>{
     return document.getElementById("demo").style.textDecoration='line-through'
};
Run Code Online (Sandbox Code Playgroud)

列表生成如下,我使用了material ui库

 <List dense={dense} >
      {items.slice(0).reverse().map(x=> (
           <ListItem key={x.id} button id="demo" onClick={completed} divider>
                 <ListItemText primary={listItems(x)}/>
                 <ListItemSecondaryAction />
           </ListItem>

       ))}                              
</List>
Run Code Online (Sandbox Code Playgroud)

从我编写的代码中,我只能删除列表的第一项。每当我添加新项目时,我唯一能够删除的项目总是第一个项目。

我正在尝试找到一种方法将其应用于列表中的所有元素

javascript strikethrough reactjs material-ui

4
推荐指数
1
解决办法
6327
查看次数

axios 调用后需要刷新页面

正如标题所述,我需要在 axios 调用函数之后刷新页面,但不是在收到承诺之前。函数触发后页面当前正在刷新,但未收到承诺。我想要做的是向 api 提交一篇博客文章,当我在最后添加 preventDefault 函数时,一切正常。

这里的功能:

handleSubmit(event){
    axios.post("https://saulvegablog.devcamp.space/portfolio/portfolio_blogs", this.buildForm(), {withCredentials:true})
    .then(response => {
      this.props.handleSuccessfullFormSubmission(response.data);
    }).catch(error => {
        console.log("handlesubmit error for blog ", error)
    })
    event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)

reactjs axios

1
推荐指数
1
解决办法
4636
查看次数