标签: ref

Why can't I pass a various numbers of references to a function?

I want to do something like this:

double a, b, c, d, e;
ParseAndWrite("{1, 2, 3}", ref a, ref b, ref c);
ParseAndWrite("{4, 5}", ref d, ref e);

-> a = 1, b = 2, c = 3, d = 4, e = 5
Run Code Online (Sandbox Code Playgroud)

However, I can not write a function like this:

private void ParseAndWrite(string leInput, params ref double[] targets)
{
   (...)
}
Run Code Online (Sandbox Code Playgroud)

This doesn't work, for some reason one can not use ref and params at the same …

.net c# ref params

10
推荐指数
1
解决办法
395
查看次数

React - 将ref从哑组件(子组件)传递给智能组件(父组件)

我有一个智能和一个哑组件,我需要引用我的智能组件中的转储组件中的元素:我可以用道具传递它吗?

Dumb:
export default (props)=>{
return(
    <input type='number' ref='element'}/>
);}

Smart:
class Parent extends Component {
    constructor(props) {
        super(props);
    }
componentDidMount() {
    const node = this.refs.element; // undefined
    }
render(){
    return <Dumb { ...this.props }/>
    }
}
Run Code Online (Sandbox Code Playgroud)

ref reactjs redux

10
推荐指数
2
解决办法
2万
查看次数

如果内部方法也接受C#7中的参数,为什么不能链接ref返回?

我正在尝试新的C#7功能,我发现了一些奇怪的东西.鉴于以下简化方案:

public struct Command
{
}

public class CommandBuffer
{
    private Command[] commands = new Command[1024];
    private int count;

    public ref Command GetNextCommand()
    {
        return ref commands[count++];
    }

    public ref Command GetNextCommand(out int index)
    {
        index = count++;
        return ref commands[index];
    }
}

public class BufferWrapper
{
    private CommandBuffer cb = new CommandBuffer();

    // this compiles fine
    public ref Command CreateCommand()
    {
        ref Command cmd = ref cb.GetNextCommand();
        return ref cmd;
    }

    // doesn't compile
    public ref Command CreateCommandWithIndex()
    { …
Run Code Online (Sandbox Code Playgroud)

c# ref c#-7.0

10
推荐指数
1
解决办法
216
查看次数

scrollIntoView使用React引用

我们正在尝试在用户关闭另一个组件时滚动到特定组件.

我们的示例与下面的示例非常类似,取自https://reactjs.org/docs/refs-and-the-dom.html#exposing-dom-refs-to-parent-components

function CustomComponents(props) {
  const items = [1,2,3,4].map((x, i) => return (
    <div ref={props.inputRef} key={i}>
     x + hello
    </div>
  )

  return (
    <div>
      { items }
    </div>
  );
}

function Parent(props) {
  return (
    <div>
      <CustomComponents inputRef={props.inputRef} />
    </div>
  );
}


class Grandparent extends React.Component {
  render() {
    return (
      <Parent
        inputRef={el => this.inputElement = el}
      />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我们正在提供大量的卡片,并希望能够通过调用滚动到特定的卡片 this.refs[elem].scrollIntoView()

但我们的问题是调用this.refs在所有级别返回一个空对象,因此我们无法附加到特定元素,然后在用户返回查看此组件时触发它.

想要一些人如何解决这个问题.

javascript dom ref reactjs react-dom

10
推荐指数
1
解决办法
7799
查看次数

如何在 React 中使用 forwardRef()?

我目前在我的 React 应用程序中收到以下错误:

不能给函数组件提供 refs。尝试访问此引用将失败。你的意思是使用 React.forwardRef() 吗?

如何使用 forwardRef() 解决此问题?

我的代码如下:

const Services: FunctionComponent = (): ReactElement => {
  const servicesRef = useRef(null);

  return (
    <Layout>
      <ServicesList ref={servicesRef} />
    </Layout>
  );
};
export default Services;



const ServicesList: React.FunctionComponent = ({ children }: Props) => {
  return (
    <section className="my-24 md:my-32">
      {children && children}
    </section>
  );
};

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

javascript ref reactjs

10
推荐指数
2
解决办法
3447
查看次数

取消订阅通过ref关键字传递给委托方法的委托?

我有以下课程:

public class Terminal : IDisposable
{
    readonly List<IListener> _listeners;

    public Terminal(IEnumerable<IListener> listeners)
    {
        _listeners = new List<IListener>(listeners);
    }

    public void Subscribe(ref Action<string> source)
    {
        source += Broadcast;
        //Store the reference somehow?
    }

    void Broadcast(string message)
    {
        foreach (var listener in _listeners) listener.Listen(message);
    }

    public void Dispose()
    {
        //Unsubscribe from all the stored sources?
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经搜索了一段时间,似乎无法存储使用ref关键字传递的参数.尝试将源参数添加到列表或将其分配给字段变量不允许它保持对实际委托的原始引用的引用; 所以我的问题是:

  • 有没有办法取消订阅所有来源而不再传递他们的参考?
  • 如果没有,为了支持它,如何更改类,但仍然通过一个方法传递委托来维护订阅?
  • 是否有可能在不使用Reflection的情况下实现它?
  • 是否有可能在没有将委托/事件包装在类中然后将类作为订阅参数传递的情况下实现它?

谢谢.

编辑:似乎没有使用包装器或反射,没有解决给定的问题.我的目的是使类尽可能地可移植,而不必将代理包装在辅助类中.谢谢大家的贡献.

c# parameters delegates ref unsubscribe

9
推荐指数
1
解决办法
470
查看次数

将值参数委托给接受ref参数的函数

为了减少我正在开发的库中的维护,我试图将类似的功能委托给单个函数.例如,假设一个具有双组件向量,其中Add函数接受by-ref args,而其他函数接受by-value args.我们的想法是简单地在by-value函数中调用by-ref函数,使其只需要维护by-ref函数.

struct Vector2
{
    public float X;
    public float Y;

    public Vector2(float x, float y)
    {
        this.X = x;
        this.Y = y;
    }

    public static void Add(ref Vector2 a, ref Vector2 b, out Vector2 result)
    {
        result.X = a.X + b.X;
        result.Y = a.Y + b.Y;
    }
    public static Vector2 Add1(Vector2 a, Vector2 b)
    {
        Add(ref a, ref b, out a);
        return a;
    }
    public static Vector2 Add2(Vector2 a, Vector2 b)
    {
        a.X += b.X;
        a.Y += …
Run Code Online (Sandbox Code Playgroud)

c# struct ref

9
推荐指数
1
解决办法
127
查看次数

Cloud Firestore:如何在我的集合查询中获取文档引用并将其映射为JSON值?

假设我收集了一些评论.每个评论对象都有一个"doc ref"给发布的用户.我需要一个查询,它将返回一个注释列表,包括每个用户引用的值,所以我的查询返回一个很好的格式化的Json注释对象.

cloud ref firebase google-cloud-firestore

9
推荐指数
3
解决办法
3万
查看次数

Ref 不适用于 Vue3 中的自定义组件

我正在使用 Vue3 和组合 API。在表单组件中,我在每个字段(子组件)上放置了引用。

\n

由于某些原因,ref自定义组件的 与refQuasar 组件的 不同。

\n

当我console.log使用ref自定义组件时,我在 DevTools 中得到以下信息:

\n
Proxy\xc2\xa0{__v_skip: true}   \n
Run Code Online (Sandbox Code Playgroud)\n

(Target 中没有任何属性)

\n

而对 Quasar 组件的引用给出了:

\n
Proxy\xc2\xa0{\xe2\x80\xa6}  \n
Run Code Online (Sandbox Code Playgroud)\n

(包含 Target 中组件的所有属性)

\n

因此,我无法使用ref来访问这些子组件的属性或方法。

\n

我什至不知道是什么__v_skip意思。\n我的自定义组件是用 定义的script setup,这可能是一个原因吗?

\n

知道如何解决这个问题吗?

\n

更新\n如果我defineExpose在子组件中使用我想使用 a 从外部访问的属性和方法ref,它确实有效。但不太方便,因为这些组件有很多道具。

\n

ref custom-component vue.js quasar vuejs3

9
推荐指数
1
解决办法
8286
查看次数

Perl数组引用并避免"arg 1到键的类型必须是哈希"错误

我有一个$subscribers可能是undef 的标量,引用HASH或引用ARRAY.我已经分配了样本值$VAR1,$VAR2$VAR3进行了测试.

我只$subscribers对它是ARRAY的引用感兴趣,其中它包含多个值.在其他情况下,我对打印任何东西都不感兴趣(例如$subscribers=$VAR2;

在Perl v5.16.2下,代码似乎运行良好; 但是,当我将它移动到运行Perl v5.8.8的目标机器时,我收到编译错误:

% ./test.pl
Type of arg 1 to keys must be hash (not private variable) at ./test.pl line 23, near "$subscribers) "
Execution of ./test.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

代码如下:

#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

my $VAR1 = undef;

my $VAR2 = {'msisdn' => '1234'};

my $VAR3 = [
  {'msisdn' => '1111'},
  {'msisdn' => '2222'}, …
Run Code Online (Sandbox Code Playgroud)

arrays perl hash ref perl-data-structures

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