javascript - 在动态 block React 中对父元素使用 ref

标签 javascript reactjs redux react-redux

你能告诉我如何正确地对父 block 进行引用吗

我只知道在virtualDom中使用选择器是错误的,我需要使用refs

如何在 addTag 和 editTags 方法中使用 refs 替换选择器?

class Home extends Component {

    editTags = (e) => {
        e.target.closest('.card-list__item--tags').classList.add('active');
    }

    addTag = (e) => {
        let tagFieldValue = e.target.closest('.card-list__item_edit').querySelector('input');
        let indexItem = e.target.closest('.card-list__item').id;
        let cardList = this.props.cards;
        cardList[indexItem].tags = cardList[indexItem].tags + ', ' + tagFieldValue.value;
        this.props.onEditTags(cardList);
        tagFieldValue.value = '';
        e.target.closest('.card-list__item--tags').classList.remove('active');
    }




    render(){
        let cardList = this.props.cards;

        return(
            <div className={'card-list'}>
                {
                    cardList.length && cardList.map((card, index) =>
                        <div id={index} className={'card-list__item'}>
                            <div className={'card-list__item_row card-list__item--tags'}>
                                <div className={'tags-wrap'} onDoubleClick={this.editTags}>
                                    {
                                        card.tags.split(', ').map((tag) =>
                                            <span>{tag}</span>
                                        )
                                    }
                                </div>
                                <div className={'card-list__item_edit'}>
                                    <input id={index} type="text" placeholder={'Add tags'}/>
                                    <button onClick={this.addTag}>Add Tag</button>
                                </div>

                            </div>
                        </div>
                    )
                }
            </div>
        )
    }
}


export default Home;

最佳答案

假设您使用的是 React 16.10.0 及更高版本,您可以像这样使用 refs:

class Home extends React.Component {
  constructor(props) {
    super(props)

    this.cardListRef = React.createRef();
  }

  addTag = (e) => {
    const cardListRefNode = this.cardListRef.current;
    console.log(cardListRefNode); // card-list node will be available here
    let tagFieldValue = e.target.closest('.card-list__item_edit').querySelector('input');
    let indexItem = e.target.closest('.card-list__item').id;
    let cardList = this.props.cards;
    cardList[indexItem].tags = cardList[indexItem].tags + ', ' + tagFieldValue.value;
    this.props.onEditTags(cardList);
    tagFieldValue.value = '';
    e.target.closest('.card-list__item--tags').classList.remove('active');
  }

  render(){
        let cardList = this.props.cards;

        return(
            <div className={'card-list'} ref={this.cardListRef}>
                {
                    cardList.length && cardList.map((card, index) =>
                        <div id={index} className={'card-list__item'}>
                            <div className={'card-list__item_row card-list__item--tags'}>
                                <div className={'tags-wrap'} onDoubleClick={this.editTags}>
                                    {
                                        card.tags.split(', ').map((tag) =>
                                            <span>{tag}</span>
                                        )
                                    }
                                </div>
                                <div className={'card-list__item_edit'}>
                                    <input id={index} type="text" placeholder={'Add tags'}/>
                                    <button onClick={this.addTag}>Add Tag</button>
                                </div>

                            </div>
                        </div>
                    )
                }
            </div>
        )
    }

但正如我所见,您正在使用选择器从输入中获取值、清除输入和切换类。 React 是出于另一个目的在 React 中创建的 (https://reactjs.org/docs/refs-and-the-dom.html#when-to-use-refs)。

在您的情况下,最好使用组件的状态来完成此类任务。您可以使用 onChange 事件从输入中获取值,然后可以将这些值存储在组件的状态中。 此外,最好将“'card-list__item'”移动到一个单独的组件并根据其状态切换其中的事件类。您可以在此处找到有关如何在 React 中使用表单的更多示例:https://reactjs.org/docs/forms.html

关于javascript - 在动态 block React 中对父元素使用 ref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505456/

相关文章:

javascript - 如何在 redux-observable 中正确使用 forkJoin?

javascript - 使用 Redux react native : state changes not showing in console

javascript - 有没有办法覆盖 unicode 字符?

javascript - 继续声明困惑

reactjs - 如何在我的 React 应用程序中添加 Intercom 脚本?

javascript - React useContext 不触发重新渲染

javascript - YouTube 全屏控制不显示

javascript - 回调可以与 Promise 一起使用吗?或者在 Node.js 中这是一种方式还是另一种方式?

javascript - Redux reducer : not sure if I am setting items (new or old) to state correctly

javascript - JS normalizr 如何向实体添加不相关的键