我正在尝试编写一个方法来接受对布尔标志的引用并修改它们.布尔值都是单独声明的(即不在可索引的数据结构中),并且方法的调用者应该能够决定修改哪些布尔值.
示例代码(这适用):
class Program
{
private static bool b1, b2, b3, b4, b5;
private static void doSomething(ref bool setTrue, ref bool setFalse, ref bool invert)
{
setTrue = true;
setFalse = false;
invert = !invert;
}
static void Main(string[] args)
{
Console.WriteLine("Pre: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5);
doSomething(ref b1, ref b3, ref b5);
Console.WriteLine("Post: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5);
}
}
Run Code Online (Sandbox Code Playgroud)
输出,如预期:
Pre: False, False, False, False, False
Post: True, …Run Code Online (Sandbox Code Playgroud) 我有一个使用EntityFramework for ORM的ASP.NET MVC 4 Web API应用程序.
在我返回的JSON中,在某些情况下,多个父节点存在相同的子节点.在这些情况下,子节点的第一次出现完全可见其所有成员.任何后续出现都会显示为第一次出现的$ ref.我希望每次在返回的JSON中显示完整的对象.
例如,而不是看到:
[{
"$id": "1",
"userId": 1,
"Badge": {
"$id": "2",
"badgeId": 1,
"badgeName": "Gold"
}
}, {
"$id": "3",
"userId": 2,
"Badge": {
"$ref": "2"
}
}]
Run Code Online (Sandbox Code Playgroud)
我想看:
[{
"$id": "1",
"userId": 1,
"Badge": {
"$id": "2",
"badgeId": 1,
"badgeName": "Gold"
}
}, {
"$id": "3",
"userId": 2,
"Badge": {
"$id": "4",
"badgeId": 1,
"badgeName": "Gold"
}
}]
Run Code Online (Sandbox Code Playgroud)
基本上我想摆脱JSON中的任何"$ ref".有办法吗?
谢谢!
我在使用ref带有样式组件的s 时遇到困难.当我尝试在我的类方法中访问它们时,我收到以下错误:
Edit.js:42 Uncaught TypeError:这.....包含不是一个函数
constructor(props) {
....
this.setWrapperRef = this.setWrapperRef.bind(this);
this.handleClickOutside = this.handleClickOutside.bind(this);
}
----------
setWrapperRef = (node) => {
this.wrapperRef = node;
}
handleEdit = (e) => {
e.preventDefault();
this.props.onEdit(this.props.id, this.state.title);
}
----------
<Wrapper onSubmit={this.handleEdit} ref={this.setWrapperRef}>
...
</Wrapper>
Run Code Online (Sandbox Code Playgroud)
我从这个问题中找到了代码
我在这做错了什么?
我定义了一个使用 ref 对象作为参数的方法。当我尝试使用引用列表调用它时,它告诉我无法从引用列表转换为引用对象。我做了很多搜索来找到答案。然而,大多数答案是“你不需要在这里参考”或者有解决方法。
即使使用 ref (Base)[Inherited],似乎也无法从“ref [Inherited]”转换为“ref [Base]”。不知道我说得对不对。
我想要的是在 set { } 块中仅写入 1 行来更改值并发送通知。有什么建议么?
class CommonFunctions
{
public static void SetPropertyWithNotification(ref object OriginalValue, object NewValue, ...)
{
if(OriginalValue!= NewValue)
{
OriginalValue = NewValue;
//Do stuff to notify property changed
}
}
}
public class MyClass : INotifyPropertyChanged
{
private List<string> _strList = new List<string>();
public List<string> StrList
{
get { return _strList; }
set { CommonFunctions.SetPropertyWithNotification(ref _strList, value, ...);};
}
}
Run Code Online (Sandbox Code Playgroud) 现在C#支持ref返回值,是否有某种方式(或替代方法)使用List<T>哪里T是值类型并从索引器获取ref?从文档中可以看出它简单地返回T,这意味着无法在不将其复制出列表,更新和重新复制的情况下更新结构.
List<T>获取ref索引器或某些替代数据结构时是否有任何方法可以使用?我知道可以用数组做到这一点,但我正在寻找一个动态可扩展数组.
我想使用 ref,但是我无法在最初为 false 的条件渲染中使用它。
constructor(props) {
super(props);
this.state = {
filterActive: false,
}
this.firstRef = React.createRef();
}
Run Code Online (Sandbox Code Playgroud)
如果我在此使用 ref :-
{this.state.filterActive ?
<label ref={this.firstRef}>Time Range</label>
:null}
Run Code Online (Sandbox Code Playgroud)
引用为空。
我正在将基于 jQuery 的项目转换为 React,并在功能组件中遇到引用问题:我有一个巨大的单词列表,每个单词都应该通过它们自己的引用进行访问,以便我可以根据用户的内容更改单独的类名正在打字(我可以使用一个巨大的状态对象来完成它,但性能会受到影响)。
如果我尝试访问引用的类列表,它是未定义的。现在我不确定 classList 在功能组件中通常不可用,或者 refs 是否未正确初始化:
const wordsRef = useRef([...Array(1000)].map(() => createRef()));
...
const displayWords = words.map((word, index) => (
<React.Fragment key={index}>
<span
ref={wordsRef.current[index]}
>
{word}
</span>{' '}
</React.Fragment>
));
...
useEffect(() => {
wordsRef.current[0].classList.add('highlight');
});
...
return (
<div className={classes.root}>
{displayWords}
</div>
);
Run Code Online (Sandbox Code Playgroud)
错误:无法读取未定义的属性“add”。
我只能找到 classList 的示例,但这可能不是在功能组件中添加/删除类的方法?
这里我定义了一个选择组件,我想在条件成立时显示它。当选择前一个选择输入的值 \xe2\x80\x8b\xe2\x80\x8b 之一时,会出现此选择字段。但这里是当新的选择字段(即我的选择组件),并且我从此选择中选择一个值时,该值不是由我的表单提交的,并且当我在提交表单后执行控制台日志时,该值不存在于我的数据中,但是值字段的名称存在,但没有值。我的控制台中有一条警告,指出:\n警告:无法为函数组件提供引用。尝试访问此引用将失败。您的意思是使用 React.forwardRef() 吗?
\n检查渲染方法Controller
我的选择组件
\nexport const SelectCompanyField = () => {\n // const [page, setPage] = useState(1);\n // const [perPage, setPerPage] = useState(10);\n const { data, error } = useSWR<ServerResponse, any>(companyUrls.base, url => getDataByParams(companyUrls.base));\n console.log("Data", data, "Error", error);\n console.log(data?.entities);\n\n\n return (\n\n <Select\n\n showSearch\n placeholder="Choisir une compagnie"\n optionFilterProp="children"\n filterOption={(input, option: any) =>\n option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0\n }\n >\n {data?.entities.map(d => (\n <option value={d.index} key={d.id} >\n {d.name}\n </option>\n ))}\n\n </Select>\n\n\n );\n};\nRun Code Online (Sandbox Code Playgroud)\n条件为真时显示的选择组件
\n<Col …Run Code Online (Sandbox Code Playgroud) 我需要将名称为test1 的ref 集中起来,设置一些值,该值放置在组件槽中(从外部)。有可能以某种方式做到这一点吗?我尝试从 $refs 或 $slots 获取,但失败了。
<template>
<div id="app">
<HelloWorld>
<input type="text" ref="test1" />
</HelloWorld>
</div>
</template>
```
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
name: 'App',
components: {
HelloWorld,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
<template>
<slot></slot>
<hr />
<input type="text" ref="test2" />
</template>
<script>
export default {
name: 'HelloWorld',
mounted() {
this.$refs.test2.value = 'test 2 …Run Code Online (Sandbox Code Playgroud) 为什么以下代码在 C# 11 中无法编译?
// Example 1 - fails
class C {
public Span<int> M(ref int arg) {
Span<int> span;
span = new Span<int>(ref arg);
return span;
}
}
Run Code Online (Sandbox Code Playgroud)
它产生两个编译错误:
错误CS9077:无法通过 ref 参数通过引用“arg”返回参数;它只能在 return 语句中返回。
错误 CS8347:无法在此上下文中使用“Span.Span(ref int)”的结果,因为它可能会在其声明范围之外公开参数“reference”引用的变量。
它们对我来说都没有意义:我的代码不会尝试arg通过 ref 参数返回,并且它不能公开arg其声明范围之外引用的变量。
对比之下,下面两段代码编译成功:
// Example 2 - succeeds
class C {
public Span<int> M(ref int arg) {
Span<int> span = new Span<int>(ref arg);
return span;
}
}
Run Code Online (Sandbox Code Playgroud)
// Example 3 - succeeds
class C {
public Span<int> …Run Code Online (Sandbox Code Playgroud)