我遵循一个带有Hooks在线课程的Advanced模式,在这个早期的例子中,他们用以下API创建了一个可扩展组件(比如经典的手风琴或可折叠面板):
<Expandable>
<Expandable.Header>This is the header</Expandable.Header>
<Expandable.Body>This is the content</Expandable.Body>
</Expandable>
他们使用上下文将州expanded传递给可扩展的孩子。到目前为止还不错:
impor
我需要实现的是,以我将在这里显示的方式,处理从一个JSX组件导入到它的父组件的特定属性。
子组件返回带有'someId‘属性的JSX:
return <h1 someId={123}>{title}</h1>
并且父程序将这个JSX分配给一个变量,然后需要从它读取这个'someId‘属性-然后将该变量挂载到DOM中:
let mySpecialChild = <Child title="Hello boys and girls!" />
let mySpecialChildId = mySpecialChild.someId
这是我的代码的一个片段。当我调用filteredData()时,状态没有更新。在状态更新之前,我必须单击该按钮两次。这是一个旧项目,我正在使用钩子更新。我之前在设置状态后使用了一个回调函数,但我不能使用钩子来做这件事。
function change(event) {
let name = event.target.name;
let value =
event.target.type === "checkbox"
? event.target.checked
: event.target.value;
setState((prevState) =
我尝试在useEffect中更新数据,但在控制台中显示警告。
React Hook useEffect has missing dependencies: 'dispatch', 'id', and 'state.selectedHotel'. Either include them or remove the dependency array react-hooks/exhaustive-deps
码
import { GlobalContext } from "../../../context/globalContext";
我有一个带有react redux的Cart组件,还有一个showProducts组件,它从useEffect中的API (使用等待-异步)获取产品,然后使用useState设置一些状态,并使用调度来更新redux状态。我一直收到这样的警告:
Warning: Cannot update a component (`Cart`) while rendering a different component (`ShowProducts`). To locate the bad setState() call inside `ShowProducts`, follow the stack trac