4
\$\begingroup\$

I have a component that I want to render with certain styling based upon the props that it will receive. I'm currently defaulting the prop types, and then creating the styling at render as such:

 getDefaultProps: function(){
 return{
 size: "small",
 shape: "rounded"
 };
},
statics : {
 function getShape(shape){
 if(shape === "rounded"){
 return "59px";
 }else{
 return "2px";
 }
 },
 function getSize(size){
 if(size === "large"){
 return "136px";
 }else if(size==="medium"){
 return "68px";
 }else{
 return "34px";
 }
 }
},
render: function(){
 var borderpx = Avatar.getShape(this.props.shape);
 var imgpx = Avatar.getSize(this.props.size);
 return(
 <img src={this.props.img} style={{"border-radius" : borderpx, height: imgpx}} />
 );
}

What I'm trying to figure out:

  1. Is this the correct way to "dynamically" render the style in React?
  2. Is there anything in my code that I could do better?
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 24, 2015 at 15:15
\$\endgroup\$

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.