简体   繁体   English

无状态组件prop函数在呈现时如何运行?

[英]How can a stateless component prop function run when it renders?

I have a long list of items and when they render, I want the scroll to move to the bottom of the list -- but only when they render. 我有一长串项目,当他们渲染时,我希望滚动移动到列表的底部 - 但只有当它们渲染时。 Does anyone know how to do this with a stateless component? 有没有人知道如何使用无状态组件执行此操作? I would like a function to run as a prop on the component and I want this function to pass the element so that in my container component with the Redux API I can scroll to the element. 我想要一个函数作为组件的prop运行,我希望这个函数传递元素,以便在我的容器组件中使用Redux API我可以滚动到该元素。 However, I only want the function to run when the component renders, and I am struggling to get a function on a component to run when it renders with itself passed as an argument. 但是,我只希望函数在组件呈现时运行,并且我很难在组件上运行时获取一个函数,当它以自身的形式作为参数传递时。

I am familiar with these events in a class component regarding the lifecycle of a component: 我熟悉关于组件生命周期的类组件中的这些事件:

  • componentWillReceiveProps() componentWillReceiveProps()
  • shouldComponentUpdate() shouldComponentUpdate()
  • componentWillUpdate() componentWillUpdate()
  • componentDidUpdate() componentDidUpdate()
  • render() 渲染()

But these lifecycle events are not available to me in a stateless component. 但是这些生命周期事件在无状态组件中是不可用的。

Stateless function components do not have a lifecycle as that would be nonsensical. 无状态功能组件没有生命周期,因为它是荒谬的。 The React lifecycle is inherently stateful. React生命周期本质上是有状态的。

You've got a few options: 你有几个选择:

  1. Convert your stateless function components to class components to make use of the lifecycle methods. 将无状态函数组件转换为类组件以使用生命周期方法。
  2. Wrap your stateless function components in a higher order component that provides the lifecycle management you need. 将无状态功能组件包装在更高阶的组件中,从而提供所需的生命周期管理。 Take a look at recompose and the lifecycle higher order component it provides. 看一下recompose和它提供的lifecycle高阶组件。

Based on your last comment, you could solve your use case like this: https://jsfiddle.net/69z2wepo/83446/ 根据您的上一条评论,您可以像这样解决您的用例: https//jsfiddle.net/69z2wepo/83446/

In the list component, you add this in the componentWillMount: 在list组件中,您可以在componentWillMount中添加它:

componentWillReceiveProps(newProps) {
    if (!this.props.comments.length && newProps.comments.length) {

      // Using setTimeout 0 will cause the function to be triggered after the re-render
      setTimeout(() => {

        // this.list being a ref on the list component 
        const { height } = this.list.getBoundingClientRect();
        document.body.scrollTop = height;
      }, 0);
    }
  }

In this case the Comment component can be stateless 在这种情况下,Comment组件可以是无状态的

function Comment(props) {
  return (
    <li>{props.comment.content}</li>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当同一组件中的道具更新时,如何运行 function? - How to run a function when a prop updates within the same component? 道具变更时,无状态组件不会重新呈现 - Stateless component is not rerendering when prop changes prop 在组件渲染时返回 Null - prop returns Null when component renders 无状态组件胖箭头函数中的道具无法使用 eslint 进行验证 - Prop in stateless component fat arrow function cannot be validated with eslint 使用无状态函数定义组件时如何初始化状态 - How can I Initialize the state when using a stateless function to define my component 类型函数的无状态功能组件支持不会更新 - Stateless Functional Component prop of type function doesn't update 如何在reactjs中调用无状态功能组件内部的函数 - How can I call a function inside of stateless functional component in reactjs 如何使用 Jest 测试无状态组件中的函数? - How can i use Jest to test a function inside a stateless component? 如何在 React 的无状态 function 组件中初始化 class 实例? - How can I initialize a class instance in a stateless function component in React? 在 React 中,如何渲染作为 prop 传入的无状态组件? - In React, how do I render a stateless component that is passed in as a prop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM