简体   繁体   English

React 使用功能组件更改父组件的内部功能

[英]React Change inner function of parent component using functional component

I have two components: one parent and one in child component as shown below:我有两个组件:一个父组件和一个子组件,如下所示:

import React, { Component, useState, useEffect } from 'react';

const useDocumentTitle = (title) => {
  useEffect(() => {
    document.title = title;
  }, [title])
}

function App(){
  const [count,setCount] = useState(0);
  const incrementCount = () => setCount(count + 1);
  const decrementCount = () => setCount(count - 1);
  const newDivElem = () => { return ( <>Hello World </>)}
  useDocumentTitle(`You clicked ${count} times`);

  return (
    <>
     Count of this value {count}
      <br />
      <button onClick={incrementCount}>+</button>&nbsp;
      <button onClick={decrementCount}>-</button>
      {newDivElem()}
    </>
  );
}

export default App; 

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

How can I overwrite the App component inner function newDivElem() inside the InternalApp component?如何覆盖 InternalApp 组件内的 App 组件内部函数newDivElem()

Please suggest some idea.请提出一些想法。

You can extract the function to a prop, and set the original function as the default value:您可以将该函数提取到一个 prop,并将原始函数设置为默认值:

const newDivElem = () => { return ( <>Hello World </>)}

function App({ newDivElem = newDivElem }){
  const [count,setCount] = useState(0);
  const incrementCount = () => setCount(count + 1);
  const decrementCount = () => setCount(count - 1);
  useDocumentTitle(`You clicked ${count} times`);

  return (
    <>
     Count of this value {count}
      <br />
      <button onClick={incrementCount}>+</button>&nbsp;
      <button onClick={decrementCount}>-</button>
      {newDivElem()}
    </>
  );
}

If you want to override it, pass another function as the prop:如果你想覆盖它,传递另一个函数作为道具:

function InternalApp(){
   return(
      <App newDivElem={() => <div>Something Else</div>} />
  );
}

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

相关问题 "在反应功能组件中使用内联箭头功能" - Using inline arrow function in react functional component 反应:在功能组件中更改 State 也更改功能组件的道具值,及其父 Class State - React: Changing State in functional component also Change functional component's props value, and its Parent Class State React 功能组件父子 - 未处理的拒绝(TypeError):X 不是 function - React Functional Component Parent and Child - Unhandled Rejection (TypeError): X is not a function 通过 React 功能组件将 function 从父级传递给子级 - Passing down function from parent to child through React functional component 从其子项触发 React 父功能组件中的 function - Triggering a function in a React parent functional component from its child React 16:使用钩子和功能组件时,从父级调用子级的 function - React 16: Call children's function from parent when using hooks and functional component 反应功能组件不在父功能组件内重新渲染 - React functional component not redering inside parent functional component .map 不是具有反应功能组件的功能 - .map is not a function with react functional component 将 function 附加到 React 中的功能组件 - Attach a function to a functional component in React 从 React 组件调用功能组件中的 function - Calling a function in functional component from a React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM