【问题标题】:function argument in component's constructor in react.jsreact.js 中组件构造函数中的函数参数
【发布时间】:2016-10-06 16:47:16
【问题描述】:

当我看到example 中的以下行时:

const SortableItem = SortableElement(({value}) => <li>{value}</li>);

那我不明白 lambda 函数在哪里 ({value}) => <li>{value}</li> 用过SortableElement 中?

有人可以赐教吗?

SortableElement 的代码:

import React, {Component, PropTypes} from 'react';
import {findDOMNode} from 'react-dom';
import invariant from 'invariant';

// Export Higher Order Sortable Element Component
export default function SortableElement (WrappedComponent, config = {withRef: false}) {
    return class extends Component {
        static displayName = (WrappedComponent.displayName) ? `SortableElement(${WrappedComponent.displayName})` : 'SortableElement';
        static WrappedComponent = WrappedComponent;
        static contextTypes = {
            manager: PropTypes.object.isRequired
        };
        static propTypes = {
            index: PropTypes.number.isRequired,
            collection: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
            disabled: PropTypes.bool
        };
        static defaultProps = {
            collection: 0
        };
        componentDidMount() {

            let {collection, disabled, index} = this.props;

            if (!disabled) {
                this.setDraggable(collection, index);
            }
        }
        componentWillReceiveProps(nextProps) {
            const {index} = this.props;
            if (index !== nextProps.index && this.node) {
                this.node.sortableInfo.index = nextProps.index;
            }
            if (this.props.disabled !== nextProps.disabled)
            {
                let {collection, disabled, index} = nextProps;
                if (disabled) {
                    this.removeDraggable(collection);
                }
                else {
                    this.setDraggable(collection, index);
                }
            }
        }
        componentWillUnmount() {
            let {collection, disabled} = this.props;

            if (!disabled) this.removeDraggable(collection);
        }
        setDraggable(collection, index){
            let node = this.node = findDOMNode(this);

            node.sortableInfo = {index, collection};

            this.ref = {node};
            this.context.manager.add(collection, this.ref);
        }
        removeDraggable(collection) {
            this.context.manager.remove(collection, this.ref);
        }
        getWrappedInstance() {
            invariant(config.withRef, 'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableElement() call');
            return this.refs.wrappedInstance;
        }
        render() {
            const ref = (config.withRef) ? 'wrappedInstance' : null;
            return (
                <WrappedComponent ref={ref} {...this.props} />
            );
        }
    }
}

【问题讨论】:

    标签: reactjs


    【解决方案1】:
    export default function SortableElement (WrappedComponent, config = {withRef: false}) {
      return class extends Component {
    
        ...
    
        render() {
          const ref = (config.withRef) ? 'wrappedInstance' : null;
          return (
            <WrappedComponent ref={ref} {...this.props} />
          );
        }
      }
    }
    

    看高阶 SortableElement 函数返回的 React Component 中的 render() 方法,lambda 函数(这是一个 stateless component)作为第一个参数传递给高阶函数,而这个第一个参数最终将成为参数WrappedComponent,您在这个高阶函数的签名中看到。

    因此,这个高阶函数将使用 render() 方法生成一个 React 组件,该方法使用/调用您刚刚传入的实际 React 组件(lambda 函数)。

    <WrappedComponent ref={ref} {...this.props} />
    

    【讨论】:

    • 谢谢! ref={ref} 需要什么?
    • 使用getWrappedInstance 方法从父组件访问包装组件。
    【解决方案2】:

    感谢({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt; in
    const SortableItem = SortableElement(({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt;);
    我们将实际渲染一个li 元素,其中value 作为道具从下面的map 方法传递。

    const SortableList = SortableContainer(({items}) => {
        return (
            <ul>
                {items.map((value, index) =>
                    <SortableItem key={`item-${index}`} index={index} value={value} />
                )}
            </ul>
        );
    });
    

    在 SortableElement 的 API 代码上下文中,重要的是它呈现 WrappedComponent (lines 67-69)。我们可以将SortableElement 视为任何其他高阶组件——包装另一个组件以提供一些额外功能的组件。在这种情况下 - lambda 函数的精美排序动画。

    【讨论】:

    • 谢谢!这有点道理,但你怎么能说这就是将要发生的事情呢?我无法从 SortableElement 的代码中真正看到这一点。
    • 我刚刚添加了更多说明。我希望它有所帮助。这也应该很有用:sitepoint.com/react-higher-order-components
    • 谢谢!所以 WrappedComponent 变量实际上就是传递过来的 lambda ?
    • 是的,SortableElement 就是 wraps
    【解决方案3】:

    简单地说 ({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt;

    的简写
    React.crateClass({
        render:function(){
            return <li>{this.props.value}</li> 
        }
    })
    

    参考pure functional component in React

    SortableElement 是一个 higher order component 包装了另一个 React 组件,例如上面的功能组件

    【讨论】:

    • 请注意,({value}) =&gt; &lt;li&gt;{value}&lt;/li&gt; 脱糖到 (props) =&gt; { var value = props.value; return &lt;li&gt;{value}&lt;/li&gt; },正如我在 #reactjs 中被告知的那样
    猜你喜欢
    • 2015-07-28
    • 2017-02-19
    • 2013-01-31
    • 1970-01-01
    • 2012-06-30
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多