admin管理员组文章数量:1332389
I need to disable PostList
ponent in its initial state.
import React from 'react';
import PostList from './PostList';
const App = () => {
return (
<div className="ui container">
<PostList />
</div>
);
};
export default App;
Whats the best way to disable (and grey out) a ponent? Possible solutions are to pass a value as props and then apply it to a ui element, However please keep in mind that PostList
may have inner nested ponents as well. Please share an example.
I need to disable PostList
ponent in its initial state.
import React from 'react';
import PostList from './PostList';
const App = () => {
return (
<div className="ui container">
<PostList />
</div>
);
};
export default App;
Whats the best way to disable (and grey out) a ponent? Possible solutions are to pass a value as props and then apply it to a ui element, However please keep in mind that PostList
may have inner nested ponents as well. Please share an example.
2 Answers
Reset to default 6Since you mentioned in a ment that instead of hiding it, you want to grey it instead. I would use the disabled state and style the ponent. Since PostList
could be nested, we don't know what the props are since you did not specify them.
Also, I assuming that you are not using styled-ponents
.
import React, { useState } from "react";
import PostList from "./PostList";
const App = () => {
const [disabled, setDisabled] = useState(true);
return (
<div className="ui container">
<PostList
style={{
opacity: disabled ? 0.25 : 1,
pointerEvents: disabled ? "none" : "initial"
}}
/>
</div>
);
};
export default App;
There are 2 different ways I like to do something like this.
One way you can do it is by using state
this.state = {
showList: false
}
and than something like
return (
{this.state.showList && <PostList />}
)
Another option is to pass the showList in state as a prop, so something like
return(
<PostList show = {this.state.showList} />
)
and than in PostList something like
return props.show && (your ponent here)
You can also use conditional classNames, so if you want that ponent shown, you can throw a className and style it how you normally would, but if not, just throw a display: none. I usually save doing that for replacing a navbar with a dropdown button on smaller screens, but it is another option
本文标签: javascriptReactJSDisabling a componentStack Overflow
版权声明:本文标题:javascript - ReactJS - Disabling a component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742290154a2447645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论