Allow providing callback prop to execute the command
See original GitHub issueIs your feature request related to a problem? Please describe.
My problem is the command property must be a function. Because of that, I must use this
in it for the object to be testable.
Describe the solution you’d like
I suggest introducing a new prop executeCommand: (command: Command) => void
which will take the command object and execute it. That way the command object can contain only data and no this
need to be used.
Additional context
An example would be:
Without executeCommand
:
function go() {
history.go(this.link);
}
const props = {
options: [
{
name: "Go to example",
link: "http://example.com",
command: go
},
],
};
With executeCommand
:
const props = {
options: [
{
name: "Go to example",
link: "http://example.com",
},
],
executeCommand: (command) => {
history.go(command.link)
}
};
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Pass a Callback as Props - React - Free Code Camp - YouTube
In this React tutorial we pass a callback as props. This video constitutes one part of many where I cover the FreeCodeCamp ...
Read more >FreeCodeCamp/pass-a-callback-as-props.english.md at master
This is how you allow child components to interact with their parent components. You pass methods to a child just like a regular...
Read more >React: how to pass arguments to the callback - Stack Overflow
What you can do is create a partially applied or higher order function to enclose the item.id and pass it along. So let's...
Read more >Create Callbacks for Graphics Objects - MATLAB & Simulink
A callback is a command that executes in response to some predefined user action, such as clicking on a graphics object or closing...
Read more >Dealing with callbacks as props in React - DEV Community
When we declare callback as onClick={this.fetchUsers} every render call will pass the same onClick reference to the button . At the time, when ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@iddan I’ve got a few other enhancements to get to first but I’m open to a pull request
Yes, you’d be using onSelect much like the browser native drop down list box and matching on the returned object so that your function could be executed.
For context, I’ve periodically considered eliminating the inclusion of functions passed to the commands prop. It’s really not required and breaks with native browser control conventions. However, I wanted a batteries included component the was easy to use.