简体   繁体   English

如何在React的功能组件中将参数传递给功能

[英]How to pass arguments to function in functional component in React

How can I pass arguments to a function in functional component? 如何将参数传递给功能组件中的函数? As far as I know, creating function in jsx is not good practice right? 据我所知,在jsx中创建函数不是一个好习惯,对吗?

function MyComponent(props) {
  function handleChange(event, data){
    console.log(event.target.value);
    console.log(data);
  }
  return <button onClick={handleClick(??)} value='Foo'>Click</button>
}

You can keep function outside of functional component , 您可以将功能保留在functional component之外,

MyComponent = (props) => {
  return <button onClick={(event, props.data) => handleChange(event, props.data)} value='Foo'>Click</button>
}
function handleChange(event, data){
  console.log(event.target.value);
  console.log(data);
}

There isnt anything particularly wrong with using a non-arrow function in React/jsx. 在React / jsx中使用非箭头功能没有什么特别的错误。 People just use arrow-functions more often so they don't have to bind the this keyword. 人们只是更频繁地使用箭头功能,因此不必绑定this关键字。

Something like this would work perfectly fine. 这样的事情会很好地工作。

function MyComponent(props) {
  function handleChange(event, data){
    console.log(event.target.value);
    console.log(props.data)
  }
  return <button onClick={(event, props.data) => handleChange(event)} value='Foo'>Click</button>
}

This will work 这会起作用

function MyComponent(props) {
  function handleChange(event, data){
    console.log(event.target.value);
    console.log(data)

  }
  return <button onClick={(event) => handleChange(event, 'Some Custom Value')} value='Foo'>Click</button>
}

Or if you only want to send the data property, you could do something like 或者,如果您只想发送data属性,则可以执行以下操作

function MyComponent(props) {
  function handleChange(data){
    console.log(data)

  }
  return <button onClick={(event) => handleChange('Some Custom Value')} value='Foo'>Click</button>
}

You can try this: 您可以尝试以下方法:

function MyComponent (props) {
  function handleClick(e) {
    console.log(e.target);
    another(props);
  }
  function another (props) {
    console.log(props);
  }

  return (
    <button onClick={handleClick}>Test</button>
  )
}

暂无
暂无

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

相关问题 如何通过钩子 function 反应功能组件 - How to pass hook function to react functional component 如何在反应中将类组件作为功能道具传递? - How to pass class component as a functional prop in react? 如何在 React 功能组件中的 onClick 上传递参数 - How to pass parameter on onClick in React Functional Component 如何在反应中将函数和对象传输到函数组件 - How to transport a function and an object to a functional component in react 如何在反应中将功能传输到功能组件 - How to transport a function to a functional component in react 将函数从容器传递到React + TypeScript中的功能组件 - Pass down function from container to functional component in React + TypeScript 如何将 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(功能组件)的内部 function 中将值传递给 state? - How can I pass the value to state in internal function on React(functional component)? 如何使用 onClick 从 JSX 组件向反应函数组件传递参数 - How to pass arguments to a react function component from a JSX component using onClick 如何通过 React Router Link 将值从功能组件传递到另一个功能组件? - How can I pass values from a functional component through a React Router Link to another functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM