admin管理员组文章数量:1180482
I am using component of material-ui from material-ui.
<Fade in={!randomizeFlag}>
<Grid>
<FormControlLabel control={<Switch onChange={this.handleStartValueFlag} ></Switch>} label="Start Value"></FormControlLabel>
<TextField type="number" label="Starting Value" value={startValue} onChange={this.handleStartValueChange} />
</Grid>
</Fade>
I want to completely hide the element Grid when the component fades out but it only disables the visibility of the component and takes up the same space( looks empty) in the DOM .How do i make the element hide after fading it using <Fade>
I am using component of material-ui from material-ui.
<Fade in={!randomizeFlag}>
<Grid>
<FormControlLabel control={<Switch onChange={this.handleStartValueFlag} ></Switch>} label="Start Value"></FormControlLabel>
<TextField type="number" label="Starting Value" value={startValue} onChange={this.handleStartValueChange} />
</Grid>
</Fade>
I want to completely hide the element Grid when the component fades out but it only disables the visibility of the component and takes up the same space( looks empty) in the DOM .How do i make the element hide after fading it using <Fade>
4 Answers
Reset to default 17<Fade in={!randomizeFlag} unmountOnExit={true}>
...
</Fade>
http://reactcommunity.org/react-transition-group/transition#Transition-prop-unmountOnExit
By default the child component stays mounted after it reaches the 'exited' state. Set unmountOnExit if you'd prefer to unmount the component after it finishes exiting.
Hiding element completely will introduce complexity since now you have to also unhide the element when Fade begins. That may interfere with fade effect.
With that said, you have few options:
Use CSS attribute selectors to apply styles:
div[opacity=0] { display: none; } div[opacity=1] { display: block; }
Use react-transition directly (since that is what
Fade
uses): https://reactcommunity.org/react-transition-group/transitionimport Transition from 'react-transition-group/Transition'; const duration = 300; const defaultStyle = { transition: `opacity ${duration}ms ease-in-out`, opacity: 0, } const transitionStyles = { entering: { opacity: 0, display: 'none' }, entered: { opacity: 1 , display: 'block'}, exited: { opacity: 0, display: 'none'}, }; const Fade = ({ in: inProp }) => ( <Transition in={inProp} timeout={duration}> {(state) => ( <div style={{ ...defaultStyle, ...transitionStyles[state] }}> I'm a fade Transition! </div> )} </Transition> );
Use
Fade
and pass handlers toTransition
, likeonExited
and set desired states in there. Fade will simply pass extra props to root Transition element so this may work. The only caveat is that you'd be triggering asetState
or similar postrender
phase which can get tricky.
enclose your custom components in divs or some html element and it should work just fine
<Fade in={!randomizeFlag}>
<div>
<Grid>
<FormControlLabel control={<Switch onChange=
{this.handleStartValueFlag} ></Switch>} label="Start Value">
</FormControlLabel>
<TextField type="number" label="Starting Value" value=
{startValue}
onChange={this.handleStartValueChange} />
</Grid>
</div>
</Fade>
Based on @Mrchief answer I got an idea for using style attribute on Fade element:
<Fade in={!randomizeFlag} style={{display: randomizeFlag ? 'none' : 'block'}}>
...
</Fade>
本文标签:
版权声明:本文标题:javascript - <Fade> in material-ui just disables the visibility of the component . How to get the fade effect and 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738127863a2065101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论