我正在创建一个应用程序,我需要在初始渲染时创建一个对象并在整个组件生命周期中保留它。
\n我的代码现在看起来像这样:
\nfunction Component() {\n const obj = useRef(new Smth());\n return (\n <div>\n <button onClick={obj.current.p}>p</button>\n <button onClick={obj.current.c}>c</button>\n </div>\n );\n};\nRun Code Online (Sandbox Code Playgroud)\nReact 文档说:
\n\n\nuseRef 返回一个可变的 ref 对象,其 .current 属性被初始化为传递的参数 (initialValue)。返回的对象将在组件的整个生命周期内持续存在。
\n
来自: https: //reactjs.org/docs/hooks-reference.html#useref
\n看来我使用得当。然而,Hooks FAQ 说:
\n\n\n您有时可能还想避免重新创建 useRef() 初始值。例如,也许您想确保某些命令式类实例仅创建一次:
\n
function Image(props) {\n // \xe2\x9a\xa0\xef\xb8\x8f IntersectionObserver is created on every render\n const ref = useRef(new IntersectionObserver(onIntersect));\n // ...\n}\n\nRun Code Online (Sandbox Code Playgroud)\n\n\nuseRef 不接受像 useState 这样的特殊函数重载。相反,您可以编写自己的函数来延迟创建和设置它:
\n
\nfunction Image(props) {\n const ref …Run Code Online (Sandbox Code Playgroud) 我有几个动态生成的素材UI按钮,当用户点击其中的任何一个时,我想知道哪个被点击了(让我们说获取name我在下面指定的属性值).怎么解决这个问题?基本上我想检索点击按钮的某些属性.这是一些代码
{that.state.items.map(function (item) {
return (<div key = {item.id}>
<FlatButton label={item.regionName} name = {item.id} primary={true} onClick={that.handleRegionClick}></FlatButton>
</div>
)
})}
handleRegionClick(e)
{
console.log(e.target.name) // This prints undefined
// If I could get here the _item.id_ which I assigned to _name_ I would be fine.
}
Run Code Online (Sandbox Code Playgroud)
PS.我在构造函数中也有这个
this.handleRegionClick = this.handleRegionClick.bind(this);
Run Code Online (Sandbox Code Playgroud) 我正在尝试在锚标记的 href 字符串中的大括号中使用变量。假设 {entry.email} 的值为 abc@xyz.com 那么我尝试这样做<a href="mailto:"+{entry.email}>{entry.email}</a>来生成锚标记
<a href="mailto:abc@xyz">abc@xyz
在下面的代码中
const rows = props.contacts.map((entry, index) => {
return (
<tr key={index}>
<td><a href="mailto:"+{entry.email}>{entry.email}</a></td>
</tr>
);
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用,甚至无法编译。我该怎么做呢?
useEffect这段代码每次窗口大小改变时都会运行,但是第二个参数useEffect是一个空数组,我想只要第二个参数与上次相比没有改变,就useEffect不会运行
import React from "react"
export default function WindowTracker() {
const [windowWidth, setWindowWidth] = React.useState(window.innerWidth)
React.useEffect(() => {
window.addEventListener("resize", function() {
setWindowWidth(window.innerWidth)
})
}, [])
return (
<h1>Window width: {windowWidth}</h1>
)
}Run Code Online (Sandbox Code Playgroud)
我试图循环这些数组,但收到此错误“严格模式下不允许旧八进制文字”
const myList =[
{
id: 01,
title: 'FrontEnd Engineer',
name : 'Sheshathri',
describtion: "simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
},
{
id: 02,
title: 'QA Engineer',
name : 'Jobin',
describtion: "It has survived not only five centuries, but also the leap into electronic typesetting, remaining …Run Code Online (Sandbox Code Playgroud)我只想切换最后一个显示“要切换 {user.id} 的文本”的 span 元素,我只想切换该特定 li 元素的此元素,而不是所有 li 元素。目前我的设置方式是,根据状态的工作原理,它将为所有 li 元素切换该 span 元素。我正在考虑通过映射函数传递索引并引用索引来切换我想要的元素,但我该怎么做呢?因为状态是为所有 li 元素设置的,所以有没有比使用状态更好的方法来做到这一点?
我知道使用 vanilla JS 我可以只使用 getElementById 并将其样式设置为隐藏或阻止。但是对于 React,我不确定处理这个问题的最佳方法,因为我被教导我们要尽可能避免 ID。然后就使用 useRef() 而言,我不确定这是否真的可以在这里工作,因为我需要为每个元素创建一个引用,而且我不知道我可以动态地做到这一点,或者这是否是最佳实践对于这个问题?
谢谢你!
import React, { useState } from 'react';
const Users = [
{ id: 1, name: 'Andy', age: 32 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Tom Hulk', age: 40 },
{ id: 4, name: 'Tom Hank', age: 50 },
{ id: 5, name: 'Audra', age: 30 },
{ …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main(void)
{
int a, b, c, n;
printf("What Fibonacci number would you like?:");
scanf("%d", &n);
if (n == 0 || n == 1)
return printf("%d", n);
else
for (c = 0; c < n; c++)
{
c = a + b;
a = b;
b = c;
}
printf("%d ", c);
return 0;
Run Code Online (Sandbox Code Playgroud)
}
我已经制定了使用Fibonacci方程的程序.但是我在编译期间遇到以下错误:
Error 1 error C4700: uninitialized local variable 'a' used d:\computer programming c++\20150923\20150923\20150923-1.c 15 1 20150923
Error 2 error C4700: uninitialized local variable 'b' …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include<string.h>
int main() {
char *ptr = NULL;
printf("%s", ptr);//The output is null
// printf("%s\n", ptr); //addition of **\n** give segmentation fault
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个printf输出:(null).但为什么第二个printf产出是: Segmentation fault (core dumped)只是添加:\n?
reactjs ×6
javascript ×5
react-hooks ×3
c ×2
arrays ×1
jsx ×1
material-ui ×1
object ×1
react-props ×1
react-redux ×1
use-effect ×1