首页 文章

关于生命周期方法和渲染反应?

提问于
浏览
0

我的问题是关于反应中的生命周期方法,现在我有这样的场景:一些数据是由父组件中的 componentDidMount 中的ajax获取的,然后这些数据作为prop传递给子组件,现在我们在孩子中,所以我们可以实现不同的生命周期方法,

componentDidMount :看不到prop的真值,例如日志中的日志返回空数组而不是所需的数组

componentWillMount :同样的 .

componentWillReceiveProps :同样如果不使用 nextProps

componentDidUpdate :记录数据返回作为prop传递的所需数据

render :返回所需数据

所以我应该总是被迫在componentDidUpdate中调用我的数据或渲染???,有没有办法在没有redux的情况下管理它?

1 回答

  • 0

    您孩子的 componentWillMountcomponentDidMount 可能只接收一个空数组(我认为这是默认值),因为Child组件在返回API结果之前被实例化,例如 <Child result={result} /> 而不是 result.length > 0 && <Child result={result} /> . Mount只发生一次然后 componentDidUpdate 发生在每次改变之后 .

    假设您只是显示传递的数据,那么在渲染中处理它是完全可以接受的 . 否则,如果您想要执行其他副作用,请检查它's completeness in the parent component (above paragraph) at which point you should be able to see the full value in the child' s componentDidMount . 或者,您可以按照建议在孩子的 componentDidUpdate 中执行完整性检查,并在那里进行处理 .

相关问题