Can't set PropTypes correctly in React bouncing between several alternatives
In my component I've used the following to set up the props, knowing that it's going to be some kind of data structure (and I was suggested that there's nothing to gain of exact definition of the object's fields).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.object),
dismiss: PropTypes.func
};
From that, I got the error as follows.
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
Based on that, I assumed that the data is a string and that my expectations were wrong (it's an object that is being served but maybe it gets serialized or something, I thought). So I change the type as shown below.
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.string),
dismiss: PropTypes.func
};
To my surprise, that produced the following error message.
Warning: Failed prop type: Invalid prop
permissions[0]
of typeobject
supplied toCompy
, expectedstring
.
I understand the expected XXX part, since I can see that declaration in the code. What buffles me is that it seems like the complaint changes back, requesting precisely the thing I had previously. What's up with that?
Then I figured that maybe the message complains in regard to the fields inside the object, so I declared a shape according to a blog as shown below. That structure matches precisely what the object looks like (two strings and an array of strings).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
code: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.string)
})),
dismiss: PropTypes.func
};
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
It looks like the computer tries to annoy me by being on the contrary to anything I type. Of course, it's obvious that's not the case. However, being fairly new to React, I can't make sense of it and I'm convinced that, since it's a quite weird behavior, someone will recognize it as something that I can try to debug. At this moment I'm stuck.
Warning: Failed prop type: Invalid prop permissions[0]
of type string
supplied to InvoiceCreationModal
, expected object
.
javascript reactjs
add a comment |
In my component I've used the following to set up the props, knowing that it's going to be some kind of data structure (and I was suggested that there's nothing to gain of exact definition of the object's fields).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.object),
dismiss: PropTypes.func
};
From that, I got the error as follows.
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
Based on that, I assumed that the data is a string and that my expectations were wrong (it's an object that is being served but maybe it gets serialized or something, I thought). So I change the type as shown below.
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.string),
dismiss: PropTypes.func
};
To my surprise, that produced the following error message.
Warning: Failed prop type: Invalid prop
permissions[0]
of typeobject
supplied toCompy
, expectedstring
.
I understand the expected XXX part, since I can see that declaration in the code. What buffles me is that it seems like the complaint changes back, requesting precisely the thing I had previously. What's up with that?
Then I figured that maybe the message complains in regard to the fields inside the object, so I declared a shape according to a blog as shown below. That structure matches precisely what the object looks like (two strings and an array of strings).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
code: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.string)
})),
dismiss: PropTypes.func
};
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
It looks like the computer tries to annoy me by being on the contrary to anything I type. Of course, it's obvious that's not the case. However, being fairly new to React, I can't make sense of it and I'm convinced that, since it's a quite weird behavior, someone will recognize it as something that I can try to debug. At this moment I'm stuck.
Warning: Failed prop type: Invalid prop permissions[0]
of type string
supplied to InvoiceCreationModal
, expected object
.
javascript reactjs
add a comment |
In my component I've used the following to set up the props, knowing that it's going to be some kind of data structure (and I was suggested that there's nothing to gain of exact definition of the object's fields).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.object),
dismiss: PropTypes.func
};
From that, I got the error as follows.
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
Based on that, I assumed that the data is a string and that my expectations were wrong (it's an object that is being served but maybe it gets serialized or something, I thought). So I change the type as shown below.
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.string),
dismiss: PropTypes.func
};
To my surprise, that produced the following error message.
Warning: Failed prop type: Invalid prop
permissions[0]
of typeobject
supplied toCompy
, expectedstring
.
I understand the expected XXX part, since I can see that declaration in the code. What buffles me is that it seems like the complaint changes back, requesting precisely the thing I had previously. What's up with that?
Then I figured that maybe the message complains in regard to the fields inside the object, so I declared a shape according to a blog as shown below. That structure matches precisely what the object looks like (two strings and an array of strings).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
code: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.string)
})),
dismiss: PropTypes.func
};
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
It looks like the computer tries to annoy me by being on the contrary to anything I type. Of course, it's obvious that's not the case. However, being fairly new to React, I can't make sense of it and I'm convinced that, since it's a quite weird behavior, someone will recognize it as something that I can try to debug. At this moment I'm stuck.
Warning: Failed prop type: Invalid prop permissions[0]
of type string
supplied to InvoiceCreationModal
, expected object
.
javascript reactjs
In my component I've used the following to set up the props, knowing that it's going to be some kind of data structure (and I was suggested that there's nothing to gain of exact definition of the object's fields).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.object),
dismiss: PropTypes.func
};
From that, I got the error as follows.
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
Based on that, I assumed that the data is a string and that my expectations were wrong (it's an object that is being served but maybe it gets serialized or something, I thought). So I change the type as shown below.
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.string),
dismiss: PropTypes.func
};
To my surprise, that produced the following error message.
Warning: Failed prop type: Invalid prop
permissions[0]
of typeobject
supplied toCompy
, expectedstring
.
I understand the expected XXX part, since I can see that declaration in the code. What buffles me is that it seems like the complaint changes back, requesting precisely the thing I had previously. What's up with that?
Then I figured that maybe the message complains in regard to the fields inside the object, so I declared a shape according to a blog as shown below. That structure matches precisely what the object looks like (two strings and an array of strings).
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
code: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.string)
})),
dismiss: PropTypes.func
};
Warning: Failed prop type: Invalid prop
permissions[0]
of typestring
supplied toCompy
, expectedobject
.
It looks like the computer tries to annoy me by being on the contrary to anything I type. Of course, it's obvious that's not the case. However, being fairly new to React, I can't make sense of it and I'm convinced that, since it's a quite weird behavior, someone will recognize it as something that I can try to debug. At this moment I'm stuck.
Warning: Failed prop type: Invalid prop permissions[0]
of type string
supplied to InvoiceCreationModal
, expected object
.
javascript reactjs
javascript reactjs
asked Nov 25 '18 at 13:33
Konrad VilterstenKonrad Viltersten
12.5k32136256
12.5k32136256
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It looks like your permissions array is made up of objects and strings.
you should be able to avoid the warning by simply declare the fields as of type PropTypes.any
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.any),
dismiss: PropTypes.func
};
alternatively you can try specifying the allowed types for your array
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
dismiss: PropTypes.func
};
or make it more flexible and specify in which case it should trigger the warning
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}),
dismiss: PropTypes.func
};
link to docs
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
add a comment |
Your Answer
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%2f53467995%2fcant-set-proptypes-correctly-in-react-bouncing-between-several-alternatives%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
It looks like your permissions array is made up of objects and strings.
you should be able to avoid the warning by simply declare the fields as of type PropTypes.any
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.any),
dismiss: PropTypes.func
};
alternatively you can try specifying the allowed types for your array
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
dismiss: PropTypes.func
};
or make it more flexible and specify in which case it should trigger the warning
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}),
dismiss: PropTypes.func
};
link to docs
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
add a comment |
It looks like your permissions array is made up of objects and strings.
you should be able to avoid the warning by simply declare the fields as of type PropTypes.any
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.any),
dismiss: PropTypes.func
};
alternatively you can try specifying the allowed types for your array
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
dismiss: PropTypes.func
};
or make it more flexible and specify in which case it should trigger the warning
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}),
dismiss: PropTypes.func
};
link to docs
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
add a comment |
It looks like your permissions array is made up of objects and strings.
you should be able to avoid the warning by simply declare the fields as of type PropTypes.any
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.any),
dismiss: PropTypes.func
};
alternatively you can try specifying the allowed types for your array
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
dismiss: PropTypes.func
};
or make it more flexible and specify in which case it should trigger the warning
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}),
dismiss: PropTypes.func
};
link to docs
It looks like your permissions array is made up of objects and strings.
you should be able to avoid the warning by simply declare the fields as of type PropTypes.any
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.any),
dismiss: PropTypes.func
};
alternatively you can try specifying the allowed types for your array
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
dismiss: PropTypes.func
};
or make it more flexible and specify in which case it should trigger the warning
Compy.propTypes = {
isOpen: PropTypes.bool,
permissions: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}),
dismiss: PropTypes.func
};
link to docs
edited Dec 14 '18 at 18:43
answered Nov 25 '18 at 13:46
KarimKarim
5,5121725
5,5121725
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
add a comment |
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
Thank you and yes, I can confirm that PropTypes.any did remove the error message. Now, the permissions array is made up of objects. Period. Each object has three fields - two strings and an array. That array is a bunch of strings. The question is answered as such and I'll check it green in 2 minutes. However, I'd also like to +1 you if you'd suggest a syntax that reflects said structure (instead of shuffling it under the carpet by any). Also, I'd like to bounty anybody who can share some comprehension on why exactly this it's not - it's too kind of weirdness can arise.
– Konrad Viltersten
Nov 25 '18 at 14:05
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
to understand what is going on you should debug it to check what's the array content at runtime, cause it seems it's made of different types, anyway i've added some alternatives
– Karim
Nov 25 '18 at 16:10
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%2f53467995%2fcant-set-proptypes-correctly-in-react-bouncing-between-several-alternatives%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