ReactJS defaultProps empty function declaration
What is the best practice defining a function defaultProps
that is empty in ReactJS?
My solution so far is either an empty arrow function or a null value. Which way would be better?
MyComponent.defaultProps = {
onClick: () => {},
onClickNull: null,
};
javascript reactjs
add a comment |
What is the best practice defining a function defaultProps
that is empty in ReactJS?
My solution so far is either an empty arrow function or a null value. Which way would be better?
MyComponent.defaultProps = {
onClick: () => {},
onClickNull: null,
};
javascript reactjs
1
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whetheronClick
is defined or not, thennull
else use the null object pattern by declaring() => {}
.
– Sung M. Kim
Jan 3 at 16:09
2
It's entirely up to you. If you want your code to know whether the function was provided, usenull
orundefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)
– T.J. Crowder
Jan 3 at 16:10
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54
add a comment |
What is the best practice defining a function defaultProps
that is empty in ReactJS?
My solution so far is either an empty arrow function or a null value. Which way would be better?
MyComponent.defaultProps = {
onClick: () => {},
onClickNull: null,
};
javascript reactjs
What is the best practice defining a function defaultProps
that is empty in ReactJS?
My solution so far is either an empty arrow function or a null value. Which way would be better?
MyComponent.defaultProps = {
onClick: () => {},
onClickNull: null,
};
javascript reactjs
javascript reactjs
asked Jan 3 at 16:06
dmraptisdmraptis
143210
143210
1
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whetheronClick
is defined or not, thennull
else use the null object pattern by declaring() => {}
.
– Sung M. Kim
Jan 3 at 16:09
2
It's entirely up to you. If you want your code to know whether the function was provided, usenull
orundefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)
– T.J. Crowder
Jan 3 at 16:10
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54
add a comment |
1
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whetheronClick
is defined or not, thennull
else use the null object pattern by declaring() => {}
.
– Sung M. Kim
Jan 3 at 16:09
2
It's entirely up to you. If you want your code to know whether the function was provided, usenull
orundefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)
– T.J. Crowder
Jan 3 at 16:10
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54
1
1
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whether
onClick
is defined or not, then null
else use the null object pattern by declaring () => {}
.– Sung M. Kim
Jan 3 at 16:09
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whether
onClick
is defined or not, then null
else use the null object pattern by declaring () => {}
.– Sung M. Kim
Jan 3 at 16:09
2
2
It's entirely up to you. If you want your code to know whether the function was provided, use
null
or undefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)– T.J. Crowder
Jan 3 at 16:10
It's entirely up to you. If you want your code to know whether the function was provided, use
null
or undefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)– T.J. Crowder
Jan 3 at 16:10
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54
add a comment |
1 Answer
1
active
oldest
votes
I don't think there is a right answer for this.
If you want to be more explicit in your code, go with the null
and check if the function is null
before calling it.
If you want to have less code, go with empty function.
The more important thing I would say, is to be consistent in the entire project.
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025892%2freactjs-defaultprops-empty-function-declaration%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think there is a right answer for this.
If you want to be more explicit in your code, go with the null
and check if the function is null
before calling it.
If you want to have less code, go with empty function.
The more important thing I would say, is to be consistent in the entire project.
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
add a comment |
I don't think there is a right answer for this.
If you want to be more explicit in your code, go with the null
and check if the function is null
before calling it.
If you want to have less code, go with empty function.
The more important thing I would say, is to be consistent in the entire project.
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
add a comment |
I don't think there is a right answer for this.
If you want to be more explicit in your code, go with the null
and check if the function is null
before calling it.
If you want to have less code, go with empty function.
The more important thing I would say, is to be consistent in the entire project.
I don't think there is a right answer for this.
If you want to be more explicit in your code, go with the null
and check if the function is null
before calling it.
If you want to have less code, go with empty function.
The more important thing I would say, is to be consistent in the entire project.
answered Jan 3 at 16:10
AnasAnas
3,92533161
3,92533161
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
add a comment |
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
My main concern was the possibility of performance issues using an empty function. However, your point of view helped me clarify it, thanks @Anas !!
– dmraptis
Jan 3 at 16:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025892%2freactjs-defaultprops-empty-function-declaration%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I think this depends on the situation whether "null" could mean something in your code. "null" means having no data, so if your code has a logic that handles differently whether
onClick
is defined or not, thennull
else use the null object pattern by declaring() => {}
.– Sung M. Kim
Jan 3 at 16:09
2
It's entirely up to you. If you want your code to know whether the function was provided, use
null
orundefined
or leave the property off the defaults. If you don't need your code to know and want to be able to use it without any checks, use the no-op function. (I usually create a single no-op function I reuse, although with modern engines I guess there's really no good reason for that...)– T.J. Crowder
Jan 3 at 16:10
The "no-op" term seems new to me. Thank you for your answer and the new dev term I gained @T.J.Crowder :p
– dmraptis
Jan 3 at 16:54