score:21
You can curry the function
handleSubmit: text => event => {
event.preventDefault()
console.log(text)
}
<form onSubmit={this.handleSubmit('react!!!')}>
...
Equivalent ES5 code
handleSubmit: function(text) {
return function(event) {
event.preventDefault()
console.log(text)
}.bind(this)
}
Your code is weird though because you're mixing ES6 and ES5 styles. If you're using ES6 imports, you might as well use ES6 in the rest of your code
import {Component} from 'react'
class NewForm extends Component {
handleSubmit (text) {
return event => {
event.preventDefault()
console.log(text)
}
}
render() {
return (
<form onSubmit={this.handleSubmit('react!!!')}>
<input type="text">
</form>
}
}
}
export default NewForm
Or better, since your Component doesn't use any state, you can use a Stateless Functional Component
import {Component} from 'react'
const handleSubmit = text=> event=> {
event.preventDefault()
console.log(text)
}
const NewForm = props=> (
<form onSubmit={handleSubmit('react!!!')}>
<input type="text">
</form>
)
export default NewForm
score:8
Yes, but you shouldn't do it. What you are currently doing is saying 'set the onSubmit handler to being the result of calling this.handleSubmit('react!!!')'. What would work instead is to go:
<form onSubmit={e => this.handleSubmit(e, "react!!!")}>
which is of course just the short form of saying:
<form onSubmit={function(e) { this.handleSubmit(e, "react!!!"); }}>
However, although this will work, what this is doing is defining a new anonymous function every time render is called. Render can be called often, so this will end up with a bunch of new functions being created all the time, which can cause perf issues as they need to be garbage collected and cleaned up.
What is better is to have a function which is capable of finding the data you need on its own. There are 2 different ways which I use to address this:
- Either store the relevant data in props, so that it can be pulled out from
this.props
Or, store the relevant data in the DOM (using HTML 5 data attributes), and then pull them out using javascript. Since you are given the event target in the event handler that is called, you can use it to pull out any data attributes you have stored. For example you might declare your form like this:
<form data-somefield={something.interesting} onSubmit={this.handleSubmit}>
which you could access in handleSubmit by going:
handleSubmit: function(e) {
console.log(e.target.dataset.somefield);
}
The caveat there being that a) storing data in the DOM like this doesn't feel very idiomatic React, and b) if you use this for onClick events with nested elements, you might find that you have to climb up the DOM hierarchy to find where your data is stored. As an example if you have a div with a data attribute and an onClick but it contains a span and an image, when you click the image it's possible that is what the onClick handler will see as it's target element, meaning you'd have to go up to the parent to find the div with the data attribute you're looking for.
So it's probably best to just include the data you want in your component's props and pull it out from there in your event handler.
Source: stackoverflow.com
Related Query
- How to pass in a second argument on ReactJS onSubmit function call
- How to pass a function as an argument to a ReactJS component in TypeScript
- How to pass an argument to a function in my backend using .fetch()? ReactJS + Node
- How to pass form values as FormData in reactjs on submit function
- How to call another component from onClick function in ReactJS
- How do you call a jQuery function on a ReactJS element from .jsx script?
- reactjs how to call method inside map function
- ReactJS V16 How to pass parent function to grandchild event handler
- how can I pass a single argument to a react function that returns a component?
- How to pass event and other arguments inside an arrow function in ReactJs
- How can I pass the argument up to the onClick function when it is called by reference inside return tag in reactjs?
- How to pass a ReactJS function as a callback to a external JS function?
- How to call a React Context function with an argument
- Need Reactjs help on how to properly pass this function from the parent to child button component
- how to pass array in grpc call using reactjs
- Reactjs How to right pass arguments to function in props
- How to prevent outer function call in reactjs
- Pass a second argument to a function that takes first argument automatically
- pass function argument to another component reactjs
- How to call a function and pass parameter in from a map function
- How to call the method inside the function (Clicking the a href to logout) using ReactJS
- How to pass an argument with multiple types as an argument to a function in Typescript?
- how I call back function using reactjs
- How to pass URL params to render function in ReactJS
- How to call a class function from another in ReactJs
- How to call prop function with an argument
- How to call Parent containers function from child component in Reactjs
- How to pass function that needs to be defined in useEffect to another component in ReactJS
- How to undo OnClick function on second click in ReactJS
- How to call Child function from Parent / Pass Props to a Parent component without using ref methods?
More Query from same tag
- Unable to type text inside of material UI TextField
- auth0 jwt authentication Error parsing token: Token used before issued
- Testing component created using styled component with jest and enzyme
- How to pass props/state through Link and Route - react.js
- css scale is not applied in react.js(next.js)
- How to implement simple react js animation with react-pose without node js?
- Error on accessing property of defined object in React when state is set in componentDidMount
- Redux - Specify which reducer from action.type when using combineReducer
- Return an array of object in a ionic reactjs component
- How could I write tests for uncovered lines in useEffect and dispatch?
- Mapping on useState object
- How do I share data between repeated elements in React?
- Change locale for Chart.js in React
- How do I merge 2 properties of 1 JSON file if the key name is the same?
- Characters filter by property value in React.js
- change background-colour of clicked button in react:js
- styled-components 'css' is not defined no-undef
- createBrowserHistory is undefined in history import for React Router DOM
- my states are empty arrays in the first render, blank page after login
- Created the dynamic radio, but it is affect others
- Add new element to DOM in React
- Issues when Hosting express with typescript and react app on Heroku
- Next JS styled components dark mode themeing
- Restrict Access (Meteor + React Router + Roles)
- why count prop does not update in Student
- Using Socket.io with typescript and react
- How to check if prevProps and nextProps are the same in React?
- webpack using font awesome
- Styling a specific word in a data gotten from an api which is populated on a screen
- React JS: How to set grandchildren states