简体   繁体   English

如何在父项更改时将道具传递给反应组件但不更新子项中的该道具?

[英]How do I pass a prop to a react component yet not update that prop in the child when parent changes?

const Parent = () => {
   const [thing, setThing] = useState('a string');

   // code to update thing

   return <Child thing={thing} />
}

const Child = props => {
   return <div>I want {props.thing} to be initial value without updating</div>
}

If I want 'thing' to be passed from parent to child but not update when parent changes it, how do I accomplish this?如果我希望“事物”从父级传递给子级,但在父级更改时不更新,我该如何实现?

I've tried useEffect, tried cloning 'thing' to a constant within Child...我尝试过 useEffect,尝试将“事物”克隆到 Child 中的常量...

I would use useEffect with the empty [] for dependencies, so that it only runs once.我会使用useEffect和空的[]作为依赖项,以便它只运行一次。

From Reactjs.org :来自Reactjs.org

If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ( [] ) as a second argument.如果你想运行一个效果并且只清理一次(在挂载和卸载时),你可以传递一个空数组( [] )作为第二个参数。 This tells React that your effect doesn't depend on any values from props or state, so it never needs to re-run.这告诉 React 你的效果不依赖于 props 或 state 的任何值,所以它永远不需要重新运行。

const Child = props => {
  let thing = props.thing;
  let [initialValue, setInitialValue] = useState("");

  useEffect(() => {
    setInitialValue(thing);
  }, []);

  return (
    <div>
      I want <strong>{initialValue}</strong> to be initial value without
      updating
    </div>
  );
};

在此处输入图像描述

CodeSandbox 代码沙盒

Maybe you can try setting the thing to a variable in the child component when it's null.当它是 null 时,也许您可以尝试将其设置为子组件中的变量。 So when the parent update, you don't have to update the child.因此,当父更新时,您不必更新子。

let childThing = null;
const Child = props => {
   if(childThing === null)
       childThing = props.thing
   return <div>I want {childThing} to be initial value without updating</div>
}

暂无
暂无

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

相关问题 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined React:为什么子组件在 prop 更改时不更新 - React: why child component doesn't update when prop changes 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop 如何在 React 中将整数值从父道具传递给子道具 - How to pass integer value from parent prop to child prop in React 如何将 React 道具从父母传递给孩子,再传递给另一个孩子? - How do I pass a React prop from Parent to Child, to another Child? 当它作为道具传递时,如何将状态从父组件更新到子组件? - How can I update the state from parent component to child component when its passed as a prop? 我如何将来自父母的道具作为上下文传递给它的孩子? - How do i pass a prop from a parent as context to its child? 如何在 React 中将 const 变量作为道具传递给组件? - How do I pass a const variable to a component as a prop in React? 如何在 React 和 Typescript 中将组件作为道具传递? - How do I pass a component as a prop in React and Typescript? 如何将“isVisibility”值作为道具传递给另一个组件(React) - How do I pass the "isVisibility" value as a prop to another component (React)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM