我正在创建一个注册页面,当用户注册时,它应该转到主页,然后显示一条消息。
我的实现方式如下:
message && <div>{message}</div>
useEffect(() => {}, [props.location.pathname.split('/')[1]])
运行应用程序时,出现以下错误:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
我在做什么错,如何解决?这是完整的代码:
app.js
const [message, setMessage] = useState({});
return (
<div className='App'>
{message && message.text && (
<div>{message.text}</div>
)}
<Switch>
<Route
path='register'
component={props => (
<Register setMessage={setMessage}/>
)}
/>
<Route
path='/'
component={({ location }) => (
<MainContent
location={location}
/>
)}
/>
</Switch>
</div>
);
register.js
const [shouldRedirect, setShouldRedirect] = useState(false);
const submitCallback = async credidentials => {
try {
// Post Data...
const message = { text: 'You Registered' };
setMessage(message);
setShouldRedirect(true);
} catch (error) {
console.log(error);
}
};
return (
{shouldRedirect && <Redirect to='/' />}
)
Home.jsx
useEffect(() => {
//FetchData
}, [urlPath]);