管理对无状态组件的关注 - 反应

时间:2017-12-10 14:55:02

标签: javascript reactjs dom

拥有一个容纳状态的容器组件。它呈现了许多无状态组件。

我希望能够访问所有DOM节点,因此我可以根据需要调用focus method

我正在尝试使用ref方法encouraged by the react documentation

我收到以下错误: Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

解决此错误的推荐方法是什么? 最好是没有额外的dom元素包装,如exra divs。 这是我的当前代码:

容器组件 - 负责呈现无状态组件。

import React, { Component } from 'react';
import StatelessComponent from './components/stateless-component.jsx'

class Container extends Component {
    constructor() {
        super()
        this.focusOnFirst = this.focusOnFirst.bind(this)
        this.state = {
            words: [
                'hello',
                'goodbye',
                'see ya'
            ]
        }
    }
    focusOnFirst() {
        this.node1.focus()
    }
    render() {
        return (
            <div>
                {
                    this.state.words.map((word,index)=>{
                        return <StatelessComponent
                        value={word}
                        ref={node => this[`node${index}`] = node}/>
                    })
                }
                <button onClick={this.focusOnFirst}>Focus on First Stateless Component</button>
            </div>
        )
    }
}

无状态组件 - 为简单起见,只需在div中显示文字。

import React from 'react';
export default function StatelessComponent(props)  {
    return <div>props.value</div>
}

2 个答案:

答案 0 :(得分:7)

无状态(功能)组件无法直接公开引用。但是,如果它们的内部组件可以使用引用,则可以通过ref forwarding将无状态组件(父级)的父(祖父)的函数作为引用传递给引用。使用该函数作为DOM元素的ref目标。现在,祖父母可以直接访问DOM元素参考。

请参阅React文档中的Exposing DOM Refs to Parent Components

const StatelessComponent = React.forwardRef((props, ref) => (
  <div>
    <input ref={ref} {...props} />
  </div>
));

class Container extends React.Component { 
  itemRefs = []

  componentDidMount() {
    this.focusOnFirst();
  }

  focusOnFirst = () => this.itemRefs[0].focus()

  inputRef = (ref) => this.itemRefs.push(ref)

  render() {
    return (
      <div>
        <StatelessComponent ref={this.inputRef} />
        <StatelessComponent ref={this.inputRef} />
      </div>
    )
  }
}

ReactDOM.render(
  <Container />,
  demo
)
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<div id="demo"></div>

答案 1 :(得分:1)

试试这个,基本上你把一个回调作为ref传递给无状态组件,它获取输入实例并将它添加到容器拥有的数组中

class Container extends Component {
    constructor() {
        super()
        this._inputs = [];
        this.focusOnFirst = this.focusOnFirst.bind(this)
        this.state = {
            words: [
                'hello',
                'goodbye',
                'see ya'
            ]
        }
    }
    focusOnFirst() {
        this._inputs[0].focus()
    }

    refCallback(ref) {
        this._inputs.push(ref)
    }

    render() {
        return (
            <div>
                {
                    this.state.words.map((word,index)=>{
                        return <StatelessComponent
                        value={word}
                        refCallback={this.refCallback}/>
                    })
                }
                <button onClick={this.focusOnFirst}>Focus on First Stateless Component</button>
            </div>
        )
    }
}

无国籍人也得到了一点修改

function StatelessComponent({refCallback, value})  {
    return <input ref={refCallback} value={value}/>
}

Here's a working plunker

相关问题