简体   繁体   English

从反应中的外部功能组件调用 function

[英]Calling a function from outside functional component in react

Say I have a form outside a functional component like this:假设我在功能组件之外有一个表单,如下所示:

   <TextField
        onChange={handleChange('name')}
        value={values.name}
        variant="outlined"
        label="Name"
        id="name"
        required
    />

And I have my component this way:我的组件是这样的:

   export default function App() {
        const handleChange = (prop) => (event) => {
           setValues({ ...values, [prop]: event.target.value });
        };
   }

How do I call the handleChange() function from inside my component?如何从组件内部调用handleChange() function?

You could pass in the handleChange function as a prop to your functional component:您可以将handleChange function 作为 prop 传递给您的功能组件:

const MyComp = (props) => {
    ...
    return <TextField
        onChange={props.handleChange('name')}
        value={values.name}
        variant="outlined"
        label="Name"
        id="name"
        required
    />
}
// Reference function here.
<MyComp handleChange={handleChange}/>

if you've a form component as a child component of a component, then you can do something like so:-如果您有一个表单组件作为组件的子组件,那么您可以这样做:-

  • ParentComponent.js :- ParentComponent.js :-
import ChildComponent from './ChildComponent

export default function ParentComponent() {
  const handleChange = (prop) => (event) => {
    setValues({ ...values, [prop]: event.target.value })
  }

  return (
    <ChildComponent 
      handleChange={handleChange} 
    />
  )
}
  • ChildComponent.js :- ChildComponent.js :-
export default function ChildComponent({handleChange}) {
  return (
    <TextField
      onChange={handleChange('name')}
      value={values.name}
      variant="outlined"
      label="Name"
      id="name"
      required
    />
  )
}

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

相关问题 在内部调用 function 从外部反应功能组件 - calling function inside react functional component from outside 从 React 组件调用功能组件中的 function - Calling a function in functional component from a React component React Native:从 class 调用功能组件内部的 function - React Native: Calling a function that's inside a functional component from a class 在 React 的功能组件之外声明 Function - Declare Function Outside of a Functional Component in React 在 React 中从父功能组件调用子功能组件方法 - Calling child functional component method from parent functional component in React React Functional 组件:作为函数调用与作为组件调用 - React Functional component: calling as function vs. as component 在反应原生外部组件中调用 function - Calling function in react native outside component 如何将 function 从 FUNCTIONAL 传递给 CLASS 组件并在渲染外部访问它(没有 prop ),在反应 js 中使用上下文? - How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js? React + TS:如何从 React 功能组件外部调用方法 - React + TS: How to call a method from outside of a React Functional Component 与反应之外的功能组件通信 - communicating with a functional component outside of react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM