0

i'm currently working on a small chat app , using reactJS an materialUI component library.

Material UI use a lot of objet to describe the style of our element (like the "sx" props).

I was wondering if i still should use css or shouhd i put my css into my component as an object (more like a vue component). here is an example with an object :

    import React from 'react'
import CameraAltIcon from '@mui/icons-material/CameraAlt'

const AvatarForm = ({setAvatar}) => {

  return (
    <Box sx={sx}>
        <label htmlFor="photo">
          <CameraAltIcon />
        </label>
        <input 
          type="file" accept="image/*" 
          id="photo"
          onChange={ e => setAvatar(e.target.files[0])}
          style={{display: "none"}}
        />
    </Box>
  )
}
    
export default AvatarForm


const sx = {
  position: "absolute",
  top: "50%",
  left: "50%",
  transform: "translate(-50%, -50%)",
  textAlign: "center",
}

Is it a good practice ? and why ?

1
  • Use CSS-in-JS: like styled-components or emotion Commented Jan 30, 2022 at 12:23

2 Answers 2

2

The official React documentation says that, whilst using inline-styles is supported, using standard css classes in generally better for performance.

Thus, in your situation I would recommend sticking to the official docs.

Sign up to request clarification or add additional context in comments.

Comments

2

if you prefer your css inside your js files, there is a new way of using it all together which called styled-components. Its really nice and have cool features.

1 Comment

Thanks for the advice, i'll check it out !

Your Answer

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.