简体   繁体   English

无法将 useState() 'set' function 传递给孙子

[英]Can't pass useState() 'set' function to grand child

I'm having issues trying to get my useState variable to work.我在尝试让我的 useState 变量工作时遇到问题。 I create the state in my grandparent then pass it into my parent.我在祖父母中创建了 state,然后将其传递给我的父母。 Here's a simplified version of my code:这是我的代码的简化版本:

export function Grandparent(){

return(
    <div>
         const [selectedID, setSelectedID] = useState("0")
         <Parent setSelectedID2={setSelectedID} .../>  //(elipses just mean that I'm passing other params too)
    <div />
)}

Parent:家长:

const Parent = ({setSelectedID2 ...}) => {

   return(
      <div>
          {setSelectedID2("5")}  //works
          <Child setSelectedID3={setSelectedID2} />
      </div>
   )
}

From the parent I can use 'setSelectedID2' like a function and can change the state.从父级我可以像 function 一样使用“setSelectedID2”,并且可以更改 state。 However, when I try to use it in the child component below I get an error stating 'setSelectedID3' is not a function.但是,当我尝试在下面的子组件中使用它时,我收到一条错误消息,指出“setSelectedID3”不是 function。 I'm pretty new to react so I'm not sure if I'm completely missing something.我对反应很陌生,所以我不确定我是否完全错过了一些东西。 Why can I use the 'set' function in parent but not child when they're getting passed the same way?为什么当他们以同样的方式通过时,我可以在父母而不是孩子中使用'set' function?

Child:孩子:

const Child = ({setSelectedID3 ...}) => {
    
    return(
        <div >
            {setSelectedID3("10")} //results in error
        </div>
    );
};

In React you make your calculations within the components/functions (it's the js part) and then what you return from them is JSX (it's the html part).在 React 中,您在组件/函数中进行计算(它是js部分),然后从它们返回的是 JSX(它是html部分)。

export function Grandparent(){

const [selectedID, setSelectedID] = useState("0");

return(
    <div>
         <Parent setSelectedID2={setSelectedID} .../>  //(elipses just mean that I'm passing other params too)
    <div />
)}

You can also use (but not define!) some js variables in JSX, as long as they are "renderable" by JSX (they are not Objects - look for React console warnings).你也可以在 JSX 中使用(但不能定义!)一些js变量,只要它们可以被 JSX“渲染”(它们不是对象——寻找 React 控制台警告)。

That's your React.101:)那是你的 React.101:)

Here's a working example with everything you have listed here.这是一个工作示例,其中包含您在此处列出的所有内容。 Props are passed and the function is called in each.传递了道具,并在每个中调用了 function。

You don't need to name your props 1,2,3.., they are scoped to the function so it's fine if they are the same.你不需要将你的道具命名为 1,2,3..,它们的范围是 function 所以如果它们相同就可以了。

I moved useState and function calls above the return statement, because that's where that logic should go in a component.我将useState和 function 调用移到了 return 语句的上方,因为那是该逻辑应该在组件中 go 的位置。 The jsx is only used for logic dealing with your display/output. jsx仅用于处理您的显示/输出的逻辑。

https://codesandbox.io/s/stupefied-tree-uiqw5?file=/src/App.js https://codesandbox.io/s/stupefied-tree-uiqw5?file=/src/App.js

Also, I created a working example with a onClick since that's what you will be doing.另外,我使用onClick创建了一个工作示例,因为这就是您将要做的。

https://codesandbox.io/s/compassionate-violet-dt897?file=/src/App.js https://codesandbox.io/s/compassionate-violet-dt897?file=/src/App.js

import React, { useState } from "react";

export default function App() {
  return <Grandparent />;
}

const Grandparent = () => {
  const [selectedID, setSelectedID] = useState("0");
  return (
    <div>
      {selectedID}
      <Parent setSelectedID={setSelectedID} selectedID={selectedID} />
    </div>
  );
};

const Parent = ({ selectedID, setSelectedID }) => {
  setSelectedID("5");
  return (
    <div>
      {selectedID}
      <Child setSelectedID={setSelectedID} selectedID={selectedID} />
    </div>
  );
};

const Child = ({ selectedID, setSelectedID }) => {
  setSelectedID("10");
  return <div>{selectedID}</div>;
};

output output

10
10
10
const [selectedID, setSelectedID] = useState("0")

should be outside return应该是外部回报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM