Transitioning to mui5's sx prop instead of makeStyles is generating a typescript error - none of the overloads match this call when passing an object with

I encountered an issue while converting a component to mui 5. Here is the original code snippet:

const useStyles = makeStyles({
   imageContainer: {
     display: "flex",
     width: "65%",
     float: "left",
     marginRight: "2px",
     position: "relative",
     zIndex: 99999,
  }
})

function MyComponent() {
  const classes = useStyles();

  return (
      <div className={classes.imageContainer}></div>
  )
}

During the conversion process, I made some changes to the code as shown below:

const imageContainer = {
     display: "flex",
     width: "65%",
     float: "left",
     marginRight: "2px",
     position: "relative",
     zIndex: 99999,
}
function MyComponent2() {
  

  return (
      <Box sx={imageContainer}></Box>
  )
}

However, I encountered an error message stating "no overload matches this call". When using inline styling with sx property, it works fine:

   <Box sx={{display: "flex",
     width: "65%",
     float: "left",
     marginRight: "2px",
     position: "relative",
     zIndex: 99999}} 
    >
        ...
    </Box>

Interestingly, if I remove the 'float' and 'position', I can use the object as planned:

const imageContainer = {
     display: "flex",
     width: "65%",
     //float: "left",
     marginRight: "2px",
     //position: "relative",
     zIndex: 99999,
}
function MyComponent2() {


  return (
      <Box sx={imageContainer}></Box>
  )
}

Should I stick with using makeStyles or should I be concerned about its deprecation in the future? Will it eventually be removed entirely? Thanks

Answer №1

In the case of using TypeScript, it is important to define it as shown below:

const imageContainer = {
    display: "flex",
    width: "65%",
    float: "left",
    marginRight: "2px",
    position: "relative",
    zIndex: 99999,
} as const;

Click here for reference

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a multi-line cell in a React data grid

Having an issue with react-data-grid where it only displays single line text instead of multi line text. Any suggestions on how to fix this? Check out the sandbox example here: https://codesandbox.io/s/5vy2q8owj4?from-embed ...

What's the best way to modify the style property of a button when it's clicked in

I am working with a react element that I need to hide when a button is clicked. The styles for the element are set in the constructor like this: constructor(props) { super(props); this.state = { display: 'block' }; this. ...

What is the best way to transfer item information from Flatlist within a React Native application?

I am a newcomer to react. In my App.js file, I have the main code, and in another folder, there is a lists.js file that contains the list data. I successfully import the list from lists.js into my App.js file. Now, I want to add an onPress action to the ...

Saving multiline text input values separately in a MySQL 5 database

Here is the input field I am using: InputArea I would like to store each input value separately. const [textInput, setTextInput] = useState('') <TextInput multiline={true} style={allStyles.textInput} placehold ...

Utilizing Smarty Template: Combining <li> elements from separate <ul> tags for a seamless display

On my Prestashop website, I have implemented the "Advanced Homepage Product List" module. I removed the category names from the titles to have my items sorted without displaying the category names. Currently, this is how it appears: https://i.stack.imgur. ...

Utilizing media queries and page width in a Moodle theme designed with Bootstrap framework

I currently have my LMS set up with Moodle 4 and the boost theme which is based on bootstrap (github) However, the default page layout appears very narrow on all devices and I would like to utilize the responsive design features of Bootstrap for different ...

Adjusting the body element's width and background hue

My goal is to adjust the background for the body element. Although I have modified the width of body, I anticipated the background to align with this width, yet it is expanding to fill the entire screen. How can I rectify this issue? body { border: 1px ...

What is the purpose of including a viewport meta tag on my website if I already have CSS media queries in place?

As someone who is just starting out with building websites, I have been delving into the world of meta tags. However, even after conducting thorough research, I am still unsure about the necessity of a meta viewport tag when I am already utilizing CSS me ...

Typescript subtraction operation may result in Undefined

I am a beginner in the world of TypeScript and I'm currently struggling with running this code snippet: class TestClass { public t: number = 10; constructor() { this.t = this.t - 1; console.log(this.t); } } var obj = new TestClass(); ...

Capture only the image contained within the specified rectangle using the Microblink SDK

Currently, I have integrated Microblink SDK with ReactJS. However, when I activate the camera to capture an image of a document, it ends up capturing the entire frame instead of just the content within the rectangle. https://i.stack.imgur.com/q0RTX.png I ...

CSS KeyFrame animations

My goal is to have my elements gracefully fade in with 2 lines extending out of a circle, one to the left and one to the right. However, I am struggling to achieve this. Every time I attempt to display the lines, it ends up shrinking the entire design. M ...

Executing string as JavaScript code in Next.js React can be achieved by using the `eval()`

I am facing an issue where I have a string that includes JavaScript code to generate a form, and I need to run that code within a React component in Next.js The variable `formJsCode` holds the dynamic JavaScript code fetched from a database. The current i ...

When a function inside React uses this.props, it won't trigger the corresponding function

I am currently developing a Checklist using React and MaterialUI that consists of two components. One component is responsible for managing the data, while the other component allows for editing the data. However, I have encountered an issue where the func ...

Why does my click button automatically update the like count when I refresh the page?

My challenge here is creating like, dislike, heart, and other buttons. The issue I'm facing is that the clicks I count need to be uploaded to the database, but I have to reload the page for it to reflect on the client side. Below is a snippet of my c ...

Having difficulty understanding Symbol.iterator and the return value type in a for-of loop while using TypeScript

Currently, I am delving into type script and embarking on a journey to learn how to craft generic containers for educational purposes. I have developed a LinkedList class with the intention of incorporating the ability to iterate over it, like so: for (co ...

Tips for creating a test to check if an HTML element is passed as a prop to a component and a function is triggered from it

What is the process for unit testing the code provided below and how can a test be written for it? I am looking to trigger the handler function in order to simulate a click from the Button that is passed to the footer prop of the Popover component. impor ...

What could be causing the delay in my scroll event with React Three Fiber?

While using React Three Fiber, I encountered an issue with a react component that generates a sprite which is supposed to remain constant in size regardless of camera zoom. Although the algorithm appears to be functioning correctly (the size does not chang ...

Tips for establishing optimal parameters for an object's dynamic property

I am working with an array of objects: export const inputsArray: InputAttributes[] = [ { label: 'Name', type: 'text', name: 'name', required: true }, { label: 'User name ...

Material User Interface, MUI, Modal, Back to Top Scroll按钮

After spending some time experimenting with scrollTop and the Dialog component (a fullscreen fixed modal) from Material-ui, I found that I couldn't quite get scrollTop to function properly. Whenever I clicked the "go down" button, it would either retu ...

Can the autocomplete list be made to scroll automatically?

I am currently using material-ui autocomplete to choose from a list of around 600 items. Does anyone know if it is feasible to have the autocomplete feature display a scrollable list of options? ...