0
0

More than 1 year has passed since last update.

React MUI - PC画面で表示するエリアはMobileで非表示表示の切り替え

Posted at

##背景

ResponsiveデザインはGridシステムを利用すれば簡単にできる。

ただ、Responsiveしつつ、PC画面ではデフォルトで表示するエリアをMobileではデフォルトで表示せず、ボタンなどのトリガーで表示させたいと思った。結果的に、ロジックは難しくないが、頭の回転が遅くて考えるのに時間がちょっとかかった。

##対策

page.js
<Grid item xs={12} sm={4}>
   <Box sx={{
      display: { xs: 'block', sm: 'none' },
   }}>
    
     <Button color="secondary" onClick={() => this.showform(this.state.xsState)}>Show/Hide Image Upload Form</Button>
                                
   </Box>
   <Box sx={{ display: { xs: `${this.state.xsState}`, sm: 'block' } }}>
       <UploadForm />
   </Box>
</Grid>

Boxのxsの値を動的に変更します。

page.js
    showform = (currentState) => {
        if (currentState == 'none'){
            this.setState({ xsState: 'block'})
        } else {
            this.setState({ xsState: 'none'})
        }
    }

はい、簡単でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0