GVKun编程网logo

如果表单不完整,请取消componentWillUnmount(表单已结束不可再填写)

213

以上就是给各位分享如果表单不完整,请取消componentWillUnmount,其中也会对表单已结束不可再填写进行解释,同时本文还将给你拓展componentDidMount或componentWi

以上就是给各位分享如果表单不完整,请取消componentWillUnmount,其中也会对表单已结束不可再填写进行解释,同时本文还将给你拓展componentDidMount 或 componentWillMount 在 React-Native 中不起作用?、javascript – 如何取消ComponentWillUnmount中的所有请求?、javascript-是否有必要在ComponentWillUnmount中卸载状态?、React componentwillmount和componentdidmount请求数据等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如果表单不完整,请取消componentWillUnmount(表单已结束不可再填写)

如果表单不完整,请取消componentWillUnmount(表单已结束不可再填写)

我有一个带有redux-form的表单设置,并且基本上想创建一个场景,如果在表单的任何输入中填充了内容,并且您尝试从页面导航离开,则会得到提示。

目的是取消页面卸载或页面导航(如果它们单击 取消) 。我尝试创建一个条件,如果满足则只是return但它仍然可以从当前页面导航。

这可能是自然的,并且我还不了解react / react-
router的工作流程,但是暂时没有人能够解释对此的最佳方法吗?如果有未满足的要求,是否有一般的东西可以让我停止卸载?

import { reduxForm } from ''redux-form'';class Form extends Component {  componentWillUnmount() {    if (!this.props.pristine && !confirm(''Are you sure you want to navigate away from this page?'')) {      return;    }  }  render() {    const { handleSubmit } = this.props;    return (      <form onSubmit={ handleSubmit(this.props.onSubmit) }>        ...      </form>    );  }}...export default connect(mapStateToProps, null)(reduxForm({  form: ''Form'',  enableReinitialize: true,  validate})(Form));

答案1

小编典典

如果您使用的是react-
router,则可以点击routerWillLeave;请参阅文档:https :
//github.com/ReactTraining/react-
router/blob/master/docs/guides/ConfirmingNavigation.md

更新

提供一个示例有点困难,这是粗糙且未经测试的。

import { reduxForm } from ''redux-form'';class Form extends React.Component {  constructor(props) {    super(props);    this.state = {      dirty: false    };  }  componentDidMount() {    this.props.router.setRouteLeaveHook(this.props.route, this.routerWillLeave.bind(this));  }  routerWillLeave(nextLocation) {    const { dirty } = this.state;    if (dirty) {      return ''You have unsaved information, are you sure you want to leave this page?''    }  }  render() {    const { handleSubmit } = this.props;    return (      <form onSubmit={ handleSubmit(this.props.onSubmit) }>        ...      </form>    );  }}

基本上,routerWillLeave将在用户尝试导航时触发。用户进行更改时,将脏状态值更新为true。该文档应涵盖您需要了解的其他内容(并确保您正在运行版本2.4.0+)。

componentDidMount 或 componentWillMount 在 React-Native 中不起作用?

componentDidMount 或 componentWillMount 在 React-Native 中不起作用?

componentDidMountcomponentWillMount 仅适用于基于类的 React 组件;您在这里拥有的是一个功能组件。您可以使用 useEffect 钩子来完成相同的操作。

useEffect(() => {
  getWeather();
},[]);

注意 this 不存在于函数组件中;您可以在声明后直接调用该函数。

如果您之前没有使用过 useEffect,您可能对数组作为第二个参数有疑问。如果它为空,它将在 mount 上运行,并将运行您从 unmount 时的第一个参数返回的内容。如果您想再次运行您的效果,请在数组中添加一个依赖项。

,

ComponentDidMount 只能在类组件中工作。使用 useEffect React hook 可以毫无问题地达到相同的效果

javascript – 如何取消ComponentWillUnmount中的所有请求?

javascript – 如何取消ComponentWillUnmount中的所有请求?

根据docs,ComponentwillUnmount能够取消请求.

我有一个页面,可以加载iframe的加载请求,以及其他页面请求.如果用户在页面仍在处理请求时决定离开页面,那么如何取消这些请求以使其不影响用户此后访问的其他页面?

我应该注意到我正在使用Redux和Redux-saga.

最佳答案
从技术上讲,您无法取消或中止承诺,因为Web浏览器不会公开任何方法.

如果用户导航到另一个页面,您可以做的只是忽略承诺.有两种方法可以做到这一点:

使用Redux,Thunk和promise中间件

您不应将应用程序状态存储在组件状态中.通过使用Redux,应用程序状态将在所有组件之间共享.然后将redux-promise-middleware与Thunk中间件结合使用,以处理Redux中的异步操作.

简单地说,在您的行动中,您可以执行以下操作:

case 'LOAD_ORDERS_SUCCESS':
  if state.location != 'orders' { return }

  // store the data in the application state

使用RxJS和observables

Observables是Promises的另一种方式.

有一个名为redux-observable的库和一个来自Netflix的video,它解释了Observable如何完全用于这个原因.在他们的文档中查看此recipe:

const fetchUserEpic = action$=>
  action$.ofType(FETCH_USER)
    .mergeMap(action =>
      ajax.getJSON(`/api/users/${action.payload}`)
        .map(fetchUserFulfilled)
        .takeuntil(action$.ofType(FETCH_USER_CANCELLED))
    );

javascript-是否有必要在ComponentWillUnmount中卸载状态?

javascript-是否有必要在ComponentWillUnmount中卸载状态?

我正在我的应用程序中componentDidMount中执行服务器请求,所以我在componentDidMount中调用setState.是否需要在componentwillUnmount中卸载此状态?这是避免我的应用程序中发生内存泄漏的解决方案吗?请帮助我找到一个解决方案.谢谢!

样例代码

componentDidMount(){
  fetch({ /* ... */ })
    .then(res => res.json())
    .then((responseData) => {
      this.setState({
        result: responseData.Meta.data
      })
    })
}

componentwillUnmount(){
  this.setState({
    result:''
  })
}
最佳答案
不需要卸载状态.将结果设置为空字符串并不比将其设置为任何其他值更好.

内存泄漏的原因是在某处使用了对对象(组件实例)的引用,这可以防止该对象作为未使用的对象被垃圾回收.

在这段代码中,可以在卸载组件后调用setState,因为请求不会被取消.这将导致警告:

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 the componentwillUnmount method.

如果请求足够长,则将导致内存泄漏.为了避免这种情况,需要取消导致setState调用的请求或承诺.至于Fetch API请求,可以用AbortController完成.

React componentwillmount和componentdidmount请求数据

React componentwillmount和componentdidmount请求数据

1.清楚调用顺序

图片描述

2.componentWillMount的问题

在componentWillMount中执行this.setState是不会触发二次渲染的。

它也只会在挂载过程中被调用一次,它的作用和constructor没有太大差异。有很多人在componentWillMount中请求后台数据,认为这样可以更早的得到数据,componentWillMout是在render函数执行前执行的,虽然请求是在第一次render之前发送的,但是返回并不能保证在render之前完成。render不会等你慢慢请求.所以在渲染的时候没有办法等到数据到来再去setState触发二次渲染.

仔细思考一下,componentWillMount好像没啥卵用了。正所谓存在即合理,在服务端渲染的场景中componentDidMount是不会被执行的,因此可以在componnetWillMount中发生AJAX请求。

顺便说一句在es6中,使用extend component的方式里的constructor函数和componentWillMount是通用的作用,所以你在构造函数里初始化了组件的状态就不必在WillMount做重复的事情了.

React中不推荐在componentWillMount中发送异步请求。

3.componentdidmount的优点

componentDidMount呢?这个生命周期函数在是在render之后调用一次,component已经初始化完成了.

在生产时,componentDidMount生命周期函数是最好的时间去请求数据,其中最重要原因:使用componentDidMount第一个好处就是这个一定是在组件初始化完成之后,再会请求数据,因此不会报什么警告或者错误,我们正常请教数据完成之后一般都会setState.

关于如果表单不完整,请取消componentWillUnmount表单已结束不可再填写的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于componentDidMount 或 componentWillMount 在 React-Native 中不起作用?、javascript – 如何取消ComponentWillUnmount中的所有请求?、javascript-是否有必要在ComponentWillUnmount中卸载状态?、React componentwillmount和componentdidmount请求数据的相关知识,请在本站寻找。

本文标签: