标签: ref

ref和redux在react-redux中引用了什么?

我正在学习文档中的 react-redux,并且没有看到以下内容.参考部分指的是什么?和节点?从我看到的那个地方都没有使用这个参考.引用后,引用是否引用DOM上的子组件节点(输入)?如果是这样,为什么不直接参考输入?

import React from 'react'
import { connect } from 'react-redux'
import { addTodo } from '../actions'

let AddTodo = ({ dispatch }) => {
  let input

  return (
    <div>
      <form onSubmit={e => {
        e.preventDefault()
        if (!input.value.trim()) {
          return
        }
        dispatch(addTodo(input.value))
        input.value = ''
      }}>
        <input ref={node => {
          input = node
        }} />
        <button type="submit">
          Add Todo
        </button>
      </form>
    </div>
  )
}
AddTodo = connect()(AddTodo)

export default AddTodo
Run Code Online (Sandbox Code Playgroud)

ref ecmascript-6 reactjs redux react-redux

8
推荐指数
1
解决办法
5259
查看次数

Vue.js - this.$refs.key 返回一个空数组或 undefined 而不是元素

我在 Vue 2 中有一个对话框窗口组件,它正在侦听事件tcs-show-confirm(它与显示对话框的事件相同,将show属性设置为 true - 在父组件中):

  @Prop({ default: false })
  show: boolean;

  @Prop()
  buttons: Array<TcsDialogButton>;

  mounted() {

    this.$nextTick(function () {
      TcsEventBus.$on('tcs-show-confirm', () => {
        console.log(this.$refs.tcsdialbuttons);
      });
    });
  }
Run Code Online (Sandbox Code Playgroud)

组件的 html 在这里(页脚插槽的内容不会被另一个组件的 html 替换):

  ...
  <slot name="footer">
    <div v-if="buttons && buttons.length" ref="tcsdialbuttons" v-for="(button, index) in buttons">
      <button tabindex="0" class="tcs-button tcs-dialog__button" @click="$emit('close', button.emitString)">
        {{button.text}}
      </button>              
    </div>
    <div style="clear: both;"></div>
  </slot>
  ...
Run Code Online (Sandbox Code Playgroud)

问题是 console.log(this.$refs.tcsdialbuttons) 显示一个空数组 [],长度为 0。这是为什么呢?如何访问按钮并将焦点设置在第一个上?


我还尝试将胖箭头功能更改为正常功能:

      TcsEventBus.$on('tcs-show-confirm', function() {
        console.log(this.$refs.tcsdialbuttons);
      });
Run Code Online (Sandbox Code Playgroud)

但现在它返回未定义。


我刚刚发现我不能引用组件中的任何元素,即使它在我的项目中的其他组件中工作。我不明白。。

javascript dom ref vue.js vue-component

8
推荐指数
1
解决办法
2557
查看次数

如果可以的话,我们应该总是通过ref返回吗?

现在有了C#7,我们可以通过ref返回return ref.根据我的收集,参考是32位或64位.现在,如果我有一个结构Coordlong Xlong Y,这将是128位,所以它会更容易通过参考返回坐标,(以及通过它),以避免复制的128位.

另一方面,如果我尝试return refa byte,只有8位,对它的引用会比复制字节本身大得多,对吧?

所以,我的主要问题是:如果我们想要返回的对象可以返回ref(即,不是局部变量)并且其大小大于引用的大小,那么我们应该返回ref吗?

编辑:快速代码示例

// This size is 128 bytes, which is 2 or 4x larger than the size of a reference
public struct Coord                                 
{
    public long X, Y;
}

private Coord myCoord;

// This will return the Coord by value, meaning copying the full 128 bytes
public Coord GetCoordValue() => myCoord;          

// This will return the …
Run Code Online (Sandbox Code Playgroud)

c# optimization ref c#-7.0 c#-7.2

8
推荐指数
1
解决办法
417
查看次数

使用React.createRef创建引用而不在React中使用构造函数?

基本上,我只用constructorReact3个原因-

1.如要初始化state-

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = { counter: 0 };
    }
}
Run Code Online (Sandbox Code Playgroud)

但是由于Babel的班级领域支持,我不再使用它

class App extends React.Component {
    state = { counter: 0 };
}
Run Code Online (Sandbox Code Playgroud)

2. bind像-

class App extends React.Component {
    constructor(props) {
        super(props);
        this.increment.bind(this);
    }

    increment() {

    }
}
Run Code Online (Sandbox Code Playgroud)

但是由于arrow功能原因,我不再这样做了

class App extends React.Component {
    increment = () => {

    }
}
Run Code Online (Sandbox Code Playgroud)

3.如要使用createRef-

class App extends React.Component {
    constructor(props) …
Run Code Online (Sandbox Code Playgroud)

javascript ref reactjs

8
推荐指数
2
解决办法
3174
查看次数

使用ref节省内存传递引用类型吗?

在C#中,方法的参数可以是引用类型或值类型.传递引用类型时,将传递引用的副本.这样,如果在方法内部我们尝试将传递的引用重新分配给另一个对象实例,则在该方法之外,重新分配是不可见的.

为了使其工作,C#具有ref修饰符.使用ref传递引用类型实际上使用原始引用而不是副本.(如我错了请纠正我).

在这种情况下,由于我们没有创建引用的副本,我们是否保存了任何内存?如果广泛调用方法,这是否会提高应用程序的整体性能?

谢谢!

.net c# ref reference-type pass-by-reference

7
推荐指数
3
解决办法
6231
查看次数

反应参考电流为 NULL

我的代码取自官方 React Ref 文档

import React from "react";
import { render } from "react-dom";

class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);
    // create a ref to store the textInput DOM element
    this.textInput = React.createRef();
    this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    // Explicitly focus the text input using the raw DOM API
    // Note: we're accessing "current" to get the DOM node
    // this.textInput.current.focus();
    console.log(this.textInput);
  }

  render() {
    // tell React that we want to associate the <input> ref
    // …
Run Code Online (Sandbox Code Playgroud)

ref reactjs

7
推荐指数
1
解决办法
6455
查看次数

ReactJS如何在Map函数内部使用ref?

我正在通过数组映射,并且为每个项目显示一个带有文本的按钮。假设我希望单击按钮,下面的文本会将其颜色更改为红色。如何定位按钮的同级?我尝试使用ref,但是由于它是映射的jsx,因此只会声明最后一个ref元素。

这是我的代码:

class Exams extends React.Component {
    constructor(props) {
        super()
        this.accordionContent = null;
    }
    state = {
        examsNames: null, // fetched from a server
    }
    accordionToggle = () => {
        this.accordionContent.style.color = 'red'
    }
    render() {
        return (
            <div className="container">
                {this.state.examsNames && this.state.examsNames.map((inst, key) => (
                    <React.Fragment key={key}>
                        <button onClick={this.accordionToggle} className="inst-link"></button>
                        <div ref={accordionContent => this.accordionContent = accordionContent} className="accordionContent">
                            <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Aperiam, neque.</p>
                        </div>    
                    </React.Fragment>
                ))}
            </div>
        )
    }
}


export default Exams;
Run Code Online (Sandbox Code Playgroud)

如所解释的,结果是在每次单击按钮时,将定位到最后一个按钮所附的段落。

提前致谢

css ref reactjs

7
推荐指数
1
解决办法
3821
查看次数

React ref.current为null

我正在使用具有可变时间范围的议程/日历应用程序。要显示当前时间的一行并显示已进行的约会块,我需要计算给定时间范围内一分钟对应多少像素。

因此,例如:如果议程在上午7点开始,在下午5点结束,则总时间为10小时。假设日历的正文高度为1000像素。这意味着每小时代表100像素,每分钟代表1.66像素。

如果当前时间是下午3点。我们距议程开始只有480分钟的路程。这意味着显示当前时间的行应该在距日历正文顶部79.68像素(480 * 1.66)的位置。

计算没有问题,但获得议程主体的高度。我当时在考虑使用React Ref来获取高度,但出现错误:ref.current is null

下面的一些代码:

class Calendar extends Component {
    calendarBodyRef = React.createRef();

    displayCurrentTimeLine = () => {
        const bodyHeight = this.calendarBodyRef.current.clientHeight; // current is null
    }

    render() {
        return (
            <table>
                <thead>{this.displayHeader()}</thead>
                <tbody ref={this.calendarBodyRef}>
                    {this.displayBody()}
                    {this.displayCurrentTimeLine()}
                </tbody>
            </table>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

ref reactjs

7
推荐指数
2
解决办法
5287
查看次数

在谈论 C# 7.2 引用扩展方法时,“this ref”和“ref this”有什么区别?

考虑以下扩展方法:

public static void Toggle(this ref bool @bool) => @bool = !@bool;

public static void Toggle2(ref this bool @bool) => @bool = !@bool;
Run Code Online (Sandbox Code Playgroud)

这些只是切换 ref 布尔变量值。测试:

class Foo
{
    private bool _flag;
    public void DoWork()
    {
        _flag.Toggle();
        Console.WriteLine(_flag);
        _flag.Toggle2();
        Console.WriteLine(_flag);
    }
}
Run Code Online (Sandbox Code Playgroud)

我们得到:

True
False
Run Code Online (Sandbox Code Playgroud)

问题:选择一种语法或另一种语法是否存在隐藏的差异?

c# extension-methods ref c#-7.2

7
推荐指数
1
解决办法
894
查看次数

Vue Composition API - 使用 TypeScript 获取引用

Vetur 在下面这一行下划线 null:

const firstRef = ref<HTMLElement>(null)
Run Code Online (Sandbox Code Playgroud)
没有重载匹配这个调用。
 Overload 1 of 3, '(raw: HTMLElement): Ref', 给出了以下错误。
  “null”类型的参数不能分配给“HTMLElement”类型的参数。
 重载 2 of 3, '(raw: HTMLElement): Ref',出现以下错误。
  “null”类型的参数不能分配给“HTMLElement”类型的参数。Vetur(2769)

这是一个浓缩的上下文。任何想法我做错了什么?

<template>
  <input id="first" ref="firstRef">
  <button type="button" @click.prevent="focusFirst">Focus</button>
</template>

<script lang="ts">
import { defineComponent, ref } from "@vue/composition-api"
export default defineComponent({
  name: "Test",
  setup() {
    const firstRef = ref<HTMLElement>(null)
    const focusFirst = () => {
      const theField = firstRef.value
      theField.focus()
    }

    return { focusFirst }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

ref typescript vue.js vue-composition-api

7
推荐指数
1
解决办法
4640
查看次数