DefaultProps structure for a React component
I am actually learning how to design React components, and I need to create a DefaultProp for a property on it. This is my code:
import React from 'react'
import PropTypes from 'prop-types'
class Component extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{this.props.element.value2}</a>
</li>
)
}
}
Component.defaultProps = { // does not set the default value when value1 is not passed
element: {value1: '#', value2: 'foo'}
}
export default Component
As my property element has two properties (value1 and value2) I find myself unable to refer to it before accesing its properties and set the default value.
I've also tried these variations, resulting all of them on an error:
Component.defaultProps = {
element.value1: '#'
}
Component.defaultProps = {
this.props.element.value1: '#'
}
reactjs react-props
add a comment |
I am actually learning how to design React components, and I need to create a DefaultProp for a property on it. This is my code:
import React from 'react'
import PropTypes from 'prop-types'
class Component extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{this.props.element.value2}</a>
</li>
)
}
}
Component.defaultProps = { // does not set the default value when value1 is not passed
element: {value1: '#', value2: 'foo'}
}
export default Component
As my property element has two properties (value1 and value2) I find myself unable to refer to it before accesing its properties and set the default value.
I've also tried these variations, resulting all of them on an error:
Component.defaultProps = {
element.value1: '#'
}
Component.defaultProps = {
this.props.element.value1: '#'
}
reactjs react-props
add a comment |
I am actually learning how to design React components, and I need to create a DefaultProp for a property on it. This is my code:
import React from 'react'
import PropTypes from 'prop-types'
class Component extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{this.props.element.value2}</a>
</li>
)
}
}
Component.defaultProps = { // does not set the default value when value1 is not passed
element: {value1: '#', value2: 'foo'}
}
export default Component
As my property element has two properties (value1 and value2) I find myself unable to refer to it before accesing its properties and set the default value.
I've also tried these variations, resulting all of them on an error:
Component.defaultProps = {
element.value1: '#'
}
Component.defaultProps = {
this.props.element.value1: '#'
}
reactjs react-props
I am actually learning how to design React components, and I need to create a DefaultProp for a property on it. This is my code:
import React from 'react'
import PropTypes from 'prop-types'
class Component extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{this.props.element.value2}</a>
</li>
)
}
}
Component.defaultProps = { // does not set the default value when value1 is not passed
element: {value1: '#', value2: 'foo'}
}
export default Component
As my property element has two properties (value1 and value2) I find myself unable to refer to it before accesing its properties and set the default value.
I've also tried these variations, resulting all of them on an error:
Component.defaultProps = {
element.value1: '#'
}
Component.defaultProps = {
this.props.element.value1: '#'
}
reactjs react-props
reactjs react-props
edited Nov 25 '18 at 14:37
Nguyen You
4,8423828
4,8423828
asked Nov 25 '18 at 13:40
BiomehanikaBiomehanika
785829
785829
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Since the propType of element is shape with value1 and value2 properties you need to set an object as default value with those properties:
Component.defaultProps = {
element: {value1: '#', value2: 'foo'}
}
Note that the default value is only used if you don't give any element prop to the component, i.e.
<Component element={{value1: '#'}}/>
will not add the value2 prop for you. If that's what you want, you need to do it yourself:
render(){
const value2 = this.props.element.value2 || 'default-value'
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{value2}</a>
</li>
)
}
And then you shouldn't specify value2 as isRequired.
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
add a comment |
You could use ES6 default arguaments. It is worth reading about them here it states:
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
An example to help explain is the following
function getInfo (name, year = 2018, color = 'blue') {
console.log(name, year, color);
}
getInfo('Chevy', 1957, 'Green'); // result --> "Chevy" 1957 "Green"
getInfo('Benz', 1975); // result --> "Benz" 1975 "blue"
getInfo('Honda'); // result --> "Honda" 2018 "blue"
I would recommend something like the following.
render() {
const {
value1 = 'defaultValue1',
value2 = 'defaultValue2'
} = this.props;
return(
<li className="nav-item">
<a className="nav-link" href={value1}>{value2}</a>
</li>
)
}
If value1 or value2 props are undefined then it will default to the value you enter.
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%2f53468048%2fdefaultprops-structure-for-a-react-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since the propType of element is shape with value1 and value2 properties you need to set an object as default value with those properties:
Component.defaultProps = {
element: {value1: '#', value2: 'foo'}
}
Note that the default value is only used if you don't give any element prop to the component, i.e.
<Component element={{value1: '#'}}/>
will not add the value2 prop for you. If that's what you want, you need to do it yourself:
render(){
const value2 = this.props.element.value2 || 'default-value'
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{value2}</a>
</li>
)
}
And then you shouldn't specify value2 as isRequired.
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
add a comment |
Since the propType of element is shape with value1 and value2 properties you need to set an object as default value with those properties:
Component.defaultProps = {
element: {value1: '#', value2: 'foo'}
}
Note that the default value is only used if you don't give any element prop to the component, i.e.
<Component element={{value1: '#'}}/>
will not add the value2 prop for you. If that's what you want, you need to do it yourself:
render(){
const value2 = this.props.element.value2 || 'default-value'
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{value2}</a>
</li>
)
}
And then you shouldn't specify value2 as isRequired.
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
add a comment |
Since the propType of element is shape with value1 and value2 properties you need to set an object as default value with those properties:
Component.defaultProps = {
element: {value1: '#', value2: 'foo'}
}
Note that the default value is only used if you don't give any element prop to the component, i.e.
<Component element={{value1: '#'}}/>
will not add the value2 prop for you. If that's what you want, you need to do it yourself:
render(){
const value2 = this.props.element.value2 || 'default-value'
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{value2}</a>
</li>
)
}
And then you shouldn't specify value2 as isRequired.
Since the propType of element is shape with value1 and value2 properties you need to set an object as default value with those properties:
Component.defaultProps = {
element: {value1: '#', value2: 'foo'}
}
Note that the default value is only used if you don't give any element prop to the component, i.e.
<Component element={{value1: '#'}}/>
will not add the value2 prop for you. If that's what you want, you need to do it yourself:
render(){
const value2 = this.props.element.value2 || 'default-value'
return(
<li className="nav-item">
<a className="nav-link" href={this.props.element.value1}>{value2}</a>
</li>
)
}
And then you shouldn't specify value2 as isRequired.
edited Nov 25 '18 at 13:57
answered Nov 25 '18 at 13:47
StefanStefan
60028
60028
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
add a comment |
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
Thank you Stefan, I have tried this with no success. It does not throw an error but neither does apply any default value when property is not passed. I have edited the question, removing the propType in order to simplify it.
– Biomehanika
Nov 25 '18 at 13:53
add a comment |
You could use ES6 default arguaments. It is worth reading about them here it states:
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
An example to help explain is the following
function getInfo (name, year = 2018, color = 'blue') {
console.log(name, year, color);
}
getInfo('Chevy', 1957, 'Green'); // result --> "Chevy" 1957 "Green"
getInfo('Benz', 1975); // result --> "Benz" 1975 "blue"
getInfo('Honda'); // result --> "Honda" 2018 "blue"
I would recommend something like the following.
render() {
const {
value1 = 'defaultValue1',
value2 = 'defaultValue2'
} = this.props;
return(
<li className="nav-item">
<a className="nav-link" href={value1}>{value2}</a>
</li>
)
}
If value1 or value2 props are undefined then it will default to the value you enter.
add a comment |
You could use ES6 default arguaments. It is worth reading about them here it states:
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
An example to help explain is the following
function getInfo (name, year = 2018, color = 'blue') {
console.log(name, year, color);
}
getInfo('Chevy', 1957, 'Green'); // result --> "Chevy" 1957 "Green"
getInfo('Benz', 1975); // result --> "Benz" 1975 "blue"
getInfo('Honda'); // result --> "Honda" 2018 "blue"
I would recommend something like the following.
render() {
const {
value1 = 'defaultValue1',
value2 = 'defaultValue2'
} = this.props;
return(
<li className="nav-item">
<a className="nav-link" href={value1}>{value2}</a>
</li>
)
}
If value1 or value2 props are undefined then it will default to the value you enter.
add a comment |
You could use ES6 default arguaments. It is worth reading about them here it states:
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
An example to help explain is the following
function getInfo (name, year = 2018, color = 'blue') {
console.log(name, year, color);
}
getInfo('Chevy', 1957, 'Green'); // result --> "Chevy" 1957 "Green"
getInfo('Benz', 1975); // result --> "Benz" 1975 "blue"
getInfo('Honda'); // result --> "Honda" 2018 "blue"
I would recommend something like the following.
render() {
const {
value1 = 'defaultValue1',
value2 = 'defaultValue2'
} = this.props;
return(
<li className="nav-item">
<a className="nav-link" href={value1}>{value2}</a>
</li>
)
}
If value1 or value2 props are undefined then it will default to the value you enter.
You could use ES6 default arguaments. It is worth reading about them here it states:
In JavaScript, function parameters default to undefined. However, it's often useful to set a different default value. This is where default parameters can help.
An example to help explain is the following
function getInfo (name, year = 2018, color = 'blue') {
console.log(name, year, color);
}
getInfo('Chevy', 1957, 'Green'); // result --> "Chevy" 1957 "Green"
getInfo('Benz', 1975); // result --> "Benz" 1975 "blue"
getInfo('Honda'); // result --> "Honda" 2018 "blue"
I would recommend something like the following.
render() {
const {
value1 = 'defaultValue1',
value2 = 'defaultValue2'
} = this.props;
return(
<li className="nav-item">
<a className="nav-link" href={value1}>{value2}</a>
</li>
)
}
If value1 or value2 props are undefined then it will default to the value you enter.
answered Nov 25 '18 at 15:08
zedukezeduke
8518
8518
add a comment |
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%2f53468048%2fdefaultprops-structure-for-a-react-component%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