小编gio*_*gim的帖子

useRef React hook 的确切行为是什么?每次重新渲染时都会重新创建对象吗?

我正在创建一个应用程序,我需要在初始渲染时创建一个对象并在整个组件生命周期中保留它。

\n

我的代码现在看起来像这样:

\n
function 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};\n
Run Code Online (Sandbox Code Playgroud)\n

React 文档说:

\n
\n

useRef 返回一个可变的 ref 对象,其 .current 属性被初始化为传递的参数 (initialValue)。返回的对象将在组件的整个生命周期内持续存在。

\n
\n

来自: https: //reactjs.org/docs/hooks-reference.html#useref

\n

看来我使用得当。然而,Hooks FAQ 说:

\n
\n

您有时可能还想避免重新创建 useRef() 初始值。例如,也许您想确保某些命令式类实例仅创建一次:

\n
\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\n
Run Code Online (Sandbox Code Playgroud)\n
\n

useRef 不接受像 useState 这样的特殊函数重载。相反,您可以编写自己的函数来延迟创建和设置它:

\n
\n
\nfunction Image(props) {\n  const ref …
Run Code Online (Sandbox Code Playgroud)

reactjs react-hooks

4
推荐指数
1
解决办法
1669
查看次数

检索单击了哪个按钮React/Material

我有几个动态生成的素材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)

javascript reactjs material-ui

3
推荐指数
1
解决办法
4202
查看次数

如何在react js中将{variable}连接到href标签?

我正在尝试在锚标记的 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)

但显然这不起作用,甚至无法编译。我该怎么做呢?

javascript reactjs react-redux

3
推荐指数
1
解决办法
6557
查看次数

为什么 useEffect 在 window.innerWidth 更改后运行,即使其中的第二个参数是空数组

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)

javascript reactjs react-hooks use-effect

3
推荐指数
1
解决办法
1372
查看次数

ReactJS 中“严格模式下不允许使用传统八进制文字”是什么意思?

我试图循环这些数组,但收到此错误“严格模式下不允许旧八进制文字”

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)

javascript arrays object reactjs react-props

2
推荐指数
1
解决办法
5365
查看次数

如何在 ReactJS 中单击时仅切换元素集合中的一个元素?

我只想切换最后一个显示“要切换 {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)

javascript jsx reactjs react-hooks

2
推荐指数
1
解决办法
1916
查看次数

如何制作斐波纳契方程?(关于未初始化变量的错误)

#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)

c

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

将NULL传递给printf%s

#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

c

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