将当前值设为空的正向引用

阿基列什·欧哈(Akhilesh ojha)

我正在尝试在演示项目中实现前向Ref,但我遇到了一个问题。来自正向ref的current的值为null,但是一旦我重新渲染NavBar组件(通过发送prop),我就会获得current的值。

我基本上需要从NavBar Component向下滚动到Home Component中存在的Section。可以直接通过提供href属性并传递ID来完成此操作。但是我想了解前向引用的工作原理以及这种方法。

有人可以帮我吗?

这是我的代码。

import './App.css';
import NavBar from './components/NavBar/NavBar';
import Home from './components/Home/Home';

class App extends Component {

  constructor(props) {
    super(props);
    this.homeRefService = React.createRef();
    this.homeRefContact = React.createRef();
  }

  render() {
    return (
      <div className="App">
        <NavBar name={this.state.name} homeRef={{homeRefService: this.homeRefService , homeRefContact: this.homeRefContact}}/>
        <Home ref={{homeRefService: this.homeRefService, homeRefContact: this.homeRefContact }}/>
      </div>
    );
  }
    
}

export default App;



**Home Component**
import React from 'react';

const home = React.forwardRef((props , ref) => {
    const { homeRefService , homeRefContact  } = ref;

    console.log(ref);
  
    return (
        <div>
            <section ref={homeRefService} id="Services">
                Our Services
            </section>

            <section ref={homeRefContact} id="Contact">
                Contact Us
            </section>
        </div>
    )
})

export default home


**NavBar Component**

import React, { Component } from 'react'

export class NavBar extends Component {


    render() {

        let homeRefs =  this.props.homeRef;

        let homeRefServiceId;
        let homeRefContactId;

        if(homeRefs.homeRefService.current) {
            homeRefServiceId = homeRefs.homeRefService.current.id;
        }
        if(homeRefs.homeRefContact.current ) {
            homeRefContactId = homeRefs.homeRefContact.current.id;
        }
        
        return (
            <div>
                <a  href={'#' + homeRefServiceId}> Our Services</a> 
                <a  href={'#' + homeRefContactId }>Contact Us</a>
            </div>
        )
    }
}

export default NavBar
东汉

仅当组件安装到DOM时,才能访问该引用。因此,您可能希望访问componentDidMount.DOM中的DOM元素。建议您将状态提升到父组件。
演示版

// App
class App extends React.Component {
  constructor(props) {
    super(props);
    this.homeRefService = React.createRef();
    this.homeRefContact = React.createRef();
    this.state = { homeServiceId: "", homeContactId: "" };
  }

  componentDidMount() {
    this.setState({
      homeServiceId: this.homeRefService.current.id,
      homeContactId: this.homeRefContact.current.id
    });
  }

  render() {
    return (
      <div className="App">
        <NavBar
          homeServiceId={this.state.homeServiceId}
          homeContactId={this.state.homeContactId}
        />
        <Home
          ref={{
            homeRefService: this.homeRefService,
            homeRefContact: this.homeRefContact
          }}
        />
      </div>
    );
  }
}

// NavBar    
export class NavBar extends Component {
  render() {
    return (
      <div>
        <a href={"#" + this.props.homeServiceId}> Our Services</a>
        <a href={"#" + this.props.homeContactId}>Contact Us</a>
      </div>
    );
  }
}

export default NavBar;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将select标签的默认选项设为Rails中name的当前值

来自分类Dev

将列表设为单独的值

来自分类Dev

将变量设为“小于”值?

来自分类Dev

将变量设为“小于”值?

来自分类Dev

JavaScript:将变量设为默认值?

来自分类Dev

将误差线设为转换后的值

来自分类Dev

将单选按钮设为默认值

来自分类Dev

Java中的正向引用

来自分类Dev

将会话值而不是destroy_session()设为空

来自分类Dev

在php中将一个值设为NULL而不是空

来自分类Dev

WhenAny为当前的空值

来自分类Dev

将空div设为一行的高度

来自分类Dev

如果找不到项目,如何将变量设为空?

来自分类Dev

非空属性引用一个瞬态值-必须在当前操作之前保存瞬态实例

来自分类Dev

当UPDATE SET时,MySQL语句总是将值设为0

来自分类Dev

如何将终端设置设为默认值?

来自分类Dev

如何使用GameKit将默认值设为0

来自分类Dev

vim:将选项卡式的行为设为默认值

来自分类Dev

如何将终端设置设为默认值?

来自分类Dev

将Yii Dropdown的默认值设为NULL

来自分类Dev

Cassandra:将集合设为布尔值,怎么办?

来自分类Dev

为什么我将数组值设为“未定义”

来自分类Dev

将主页设为默认值的 JavaScript 选项卡

来自分类Dev

单击按钮时如何将输入值设为空白?

来自分类Dev

如何将列值设为智能匹配的数字范围

来自分类Dev

将 Gimp 设为 xcf 文件的默认值,而不是 Krita

来自分类Dev

使用 MongoDB 将布尔值的总和设为整数

来自分类Dev

将在 disconnectedCallback 中从 DOM 中删除的自定义元素的所有引用设为空

来自分类Dev

使用日期时如何将当前时间设为 09:04:15 而不是 9:4:15?

Related 相关文章

热门标签

归档