我是 React 新手,正在尝试一个接一个地运行函数。这是我的代码:submit = () => {this.props.toggle();this.props.getValue();this.notify();this.props.resetValidation();this.props.disable();this.props.actionCost();};这里 getValue 是一个异步函
2021-10-25
async function unsubscribeUserHandler() {const unsubscribe = await fetch("/api/stripe-sessions/cancel-subscription", {method: "PATCH",body: JSON.stringify(),headers: {"Content-Type": "application/json
2022-03-16
我有两个 React 类组件。Component1是Component2的父级,我有两个函数(functionA 和 B),它们通过 props 从Component1传递到Component2。嵌套在Component2中,我有一个函数saveAndNotify(),它应该仅在 functionA 完全执行完毕后才执行functionA,然后执行functionB。有关更多上下文,functio
2022-04-14
我正尝试从我的react应用程序中的API获取数据。我有以下组件ListAppimport React, {Component} from 'react';import Card from './Card'export default class ListApp extends Component{constructor(props){super(props);this.state = {item
2020-12-22
我在使用这个小测试组件时遇到了麻烦,我遗漏了什么?这只是一个简单的小问题,似乎映射在哪里中断了。我不知道我是否错误地传递了值,或者是什么import React from "react";export default function App() {const [state, setState] = React.useState({input: "",items: [],error: false}
2022-01-24
我正在尝试找到组件的高度(以像素为单位),因为它的高度将在 CSS 中设置为 100%import React, { useState, useLayoutEffect, useRef } from "react";function Scale() {const [height, setHeight] = useState(0);const ref = useRef(null);useLayout
2020-04-14
嘿,我正在尝试制作一个基本的 React 滚动到顶部按钮,我从 w3 获得了此代码,我认为它很好,我尝试更改一些东西,但没有成功,我相信解决方案很愚蠢,希望得到帮助,谢谢。我收到此错误:Cannot set property 'display' of undefined这是我的代码:import React, { useRef } from "react";const ScrollToTop =
2020-09-29
我需要 React 的 ref clientHeight 报告组件的高度,但我遇到了这种奇怪的行为,即同一 ref 会根据访问方式报告不同的 clientHeight 值。如果我在 div 内输出文本,ref 会始终输出正确的高度。 const test = useRef(null)useEffect(() => {console.log(test)console.log(test.curren
我需要动态定义 HTML 元素的大小并将其高度设置为组件。我尝试使用 useRef 执行此操作,但由于状态包含前一个值(而不是当前值),因此它无法按预期工作。有人可以帮我吗?这是链接:CodeSandBoxhttps://codesandbox.io/s/happy-water-fzqk8?file=/src/App.js下面的代码运行正常,但有一个硬核变量 HEIGHT 定义选项卡的高度。我
2021-10-16
我尝试使用对输入元素的引用,然后使用inputRef.current.value获取其值但我在inputRef.current处收到错误Object is perhaps 'null'.ts(2531)。我尝试了几种不同的解决方案,但尚未使其发挥作用。任何帮助都值得赞赏。interface RefObject<T> {readonly current: T | null}const Uncontr
2021-11-23
我在 useEffect 中有一个代码(如 componentDidMount):useEffect(()=>{const idChat = props.match.params.id;const data = {idChat,user: props.user._id}authService.getChat(data).then(res =>{const userChat = res.data.u
2019-09-24
我有一个问题,如何告诉 useEffect 等待来自搜索输入的文本?API 包含许多对象,我想获取其中一个。import React, { useState, useEffect } from 'react';import axios from 'axios';const Search = () => {const [text, setText] = useState('');const [obj
2020-04-16
如何让一个函数等待另一个正在更新状态的函数?示例:提交时,表单调用trainData。TrainData 需要先处理。它使用来自props的data数组,该数组包含数据。问题:在trainData的 if 检查中,inputMatrix和outputMatrix都为空(我猜是因为他们需要先更新,但 trainData 不会等待这一点)。如何解决这个问题?我想我需要使用useEffect,但我到底在
2020-07-06
如果内容仍在加载,我尝试在等待 1.5 秒后呈现加载消息:useEffect(() => {setTimeout(() => {if (loading) setSleepMessage("The server seems to be sleeping. Waking it up, wait a second...");}, 1500);}, [loading]);变量loading是 Apollo
2020-08-13
const initData = {name: "",class: ""}const [currentStudent, setCurrentStudent] = useState({});useEffect(() => {// My example code to setStatesetCurrentStudent(exampleData)//The result of currentStuden
2020-10-15