问题标签 [react-lifecycle]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
959 浏览

reactjs - React Native:如何使用 ListView 处理生命周期方法的弃用?

我目前正在学习 React Native。我想写一个ListView。我正在遵循的教程使用不推荐使用的方法componentWillMount,现在称为UNSAFE_componentWillMount. 我用谷歌搜索了一个人说应该用componentDidMount. 我的问题是当我将此方法添加到我的代码中时,应用程序会中断。

这是代码:

这是我收到的错误消息TypeError: Cannot read property 'rowIdentities' of undefined。我应该在这里使用哪种方法,或者我该如何解决这个问题?

0 投票
1 回答
34 浏览

javascript - React setState to value received from http response

First of all, please excuse me english, i am not a native speaker. I will try to explain my best.

I am trying to set the state of my component to a value received from http request that runs on componentDidMount(). After setState, i am passing props to my child components and expect them to rerender.

So that got me the AuthState object when i console logged it but it did not rerender my component.

But if i take whats inside my getAuthFromServerFunction and put it in my componentDidMount like this..

It Works.. and my component rerenders

I am completely stumped trying to figure this out.. Any help would be greatly appreciated, thank you

0 投票
2 回答
1634 浏览

reactjs - getDerivedStateFromProps 不适用于我的应用程序。如何替换 componentWillRecieveProps?

我正在使用 Redux 创建一个 React 应用程序,在我的帐户设置页面上,我有一个表单,我希望预先填充用户的信息,然后让他们能够编辑字段并更新他们的个人资料。我的代码如下:

this.props.getCurrentProfile() 向 mongoDB 发送 axios 请求,接收当前用户的 profile。

问题是如果我使用 getDerivedStateFromProps 方法,文本字段是静态的,无法编辑。即使使用 componentWillMount 方法,如果我从浏览器刷新页面说配置文件为空,我也会收到错误消息。

我对 React 和生命周期方法非常陌生,非常感谢任何关于我做错了什么以及为什么 componentWillReceiveProps 被弃用的反馈。

提前致谢

0 投票
2 回答
52 浏览

javascript - 将获取的数据作为道具(JSON)传递到状态以呈现数据

目前,我在运行我的 saga 时以 JSON 格式从 API 获取数据。当组件挂载时,获取过程开始。这意味着组件渲染两次。

现在,当数据作为道具可用时。我可以使用它来渲染它。

我的方法如下,我有一个:

  1. 具有初始状态的构造函数
  2. 我在“componentDidMount”中获取数据
  3. 我有一个函数,它从 props 中获取 JSON 属性并将其放入新变量中
  4. 当道具包含获取的数据时,我在我的 render() 函数中运行此函数

这种方法的问题:一旦组件运行数据变成“结构化”的函数,渲染函数就会循环,然后在一段时间后,属性的值会在控制台中显示警告消息。

我的问题

  1. render() 运行一次时如何防止循环?
  2. 我该如何设计这个,以便获取对象的特定属性合并到一个新对象中,以及如何

我希望我描述了关于我的问题的最重要的事情。这是代码:

0 投票
2 回答
1786 浏览

react-native - 如何在组件仍使用 React Navigation 呈现时更新标题?

我正在编写一个 React Native 应用程序,并且我正在使用 React Navigation (V2)。在我的组件更新后,我想更新navigationOptions并添加一个新按钮。这是我尝试过的代码:

这种方法的问题是,navigationOptionscomponentDidUpdate(). 如何使用 React Navigation 动态调整标题?

0 投票
1 回答
117 浏览

reactjs - 没有在 componentDidMount 中调用 Curried 函数

在这个反应组件中,当组件安装时,onRefresh 函数根本不会被调用。如果我将此函数用于 RefreshControl,那么它将被调用。

感谢有人能告诉我为什么这没有在 componentDidMount 中被调用,而是适用于 RefreshControl。

0 投票
1 回答
693 浏览

javascript - 在 React + Redux 中使用新的生命周期方法通过更改 id 来获取数据

React我在<= 16.2中这样做了:

该组件已连接,并且fetchDataredux-thunk管理简单状态的操作创建者:{ isLoading: ..., data }. 我们可以在后续请求中添加错误处理和取消,但目前超出了范围。

其基础是组件在每次 id prop 更改时都请求它自己的数据,但每次 id prop 更改时只请求一次。

我不知道它是如何在React16.4 中正确完成的,这getDerivedStateFromProps是一个静态同步的东西,它返回新的状态。

bvaughn 在 github 上有一个很长的评论,但是它直接将数据存储在 state 中,并在数据为 null 时启动 refetch。但是,如果我将数据存储在 中redux,我不能只是将我的数据清空getDerivedStateFromProps(或者我可以吗?)。

有人可以解释我如何在“现代”中做同样的事情React吗?

0 投票
1 回答
45 浏览

reactjs - 触发 2 个 Redux Actions ,仅触发 1 个动作

我已经在以下方面采取了行动componentDidMount

并且想在完成后触发另一个动作,我使用了生命周期方法componentWillReceiveProps

然而,只有this.props.getQuests();被解雇,第二个动作this.props.getProfile(authToken)没有被解雇。

目前它返回以下错误:

0 投票
1 回答
1555 浏览

javascript - 在 Reactjs 中比较许多 prevProps 和 nextProps 的最佳方法是什么?

子组件正在从其父组件接收许多更新的道具。如果道具中有任何更新,我想更新子组件。目前,我正在使用componentWillReceiveProps按预期工作的生命周期方法进行更新。

但是,我将来会得到 8 个以上的道具。我将如何继续。谢谢。

0 投票
2 回答
7726 浏览

reactjs - 如何在我的 fetch 在 react.js 的 componentDidMount() 中执行之前通过 navigator.geolocation 获取用户的位置?

我已经尝试了各种不同的方法,但我被难住了。在 react 中使用 Promise 和进行 api 调用的新手。这就是我目前所拥有的:

现在,在获取当前位置之前正在执行 api 调用。我尝试了各种方法,但在 fetch 执行之前无法访问当前位置。处理这个问题的最佳方法是什么?对此的任何见解将不胜感激。有任何问题,请告诉我。谢谢。