REACT:如何在具有特定类名的 div 中渲染组件?
我不确定执行此操作的最佳方法是什么,但是 - 我已经输出了 div 列表。当我点击其中一个时,它会被赋予一个active
类。单击 div 后,我需要在该特定类中显示我制作(用于渲染)的组件。我正在使用 next.js 和 React 并一直在使用 CSS 模块。
目前我尝试过: ReactDOM.render(
但我收到错误错误:目标容器不是 DOM 元素。
Child (InnerDisc) Component
const InnerDisc = (props) => {
const active = props.active;
if (active) {
return (
<div className={styles.wrapper}>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
</div>
);
}
}
export default InnerDisc;
主要应用程序组件
export default function Home() {
const [discs, setDiscs] = useState([
{ id: 1, top: 100 },
{ id: 2, top: 200 },
{ id: 3, top: 300 },
{ id: 4, top: 400 },
{ id: 5, top: 500 },
{ id: 6, top: 600 },
{ id: 7, top: 700 },
{ id: 8, top: 800 },
{ id: 9, top: 900 },
{ id: 10, top: 1000 },
{ id: 11, top: 1100 },
{ id: 12, top: 1200 }
])
function enlargeDisc(e, num) {
let t = e.target
if (t.classList.contains(styles.active)) {
t.classList.remove(styles.active)
discRender()
} else {
t.classList.add(styles.active)
discRender()
}
}
function discRender() {
ReactDOM.render(
<InnerDisc/>, document.getElementsByClassName(styles.active)
);
}
return (
<div className={styles.container}>
<div className={styles.wrapper}>
{discs.map((item) => (
<div className ={styles.disc} key={item.id} style=.
{{top: item.top + 'px'}} onClick={(e)=>
enlargeDisc(e, item.id)}> </div>
))}
</div>
</div>
</div>
)
}
I'm not sure what is the best method to do this but - I have outputted list of divs. When I click on one of them, it gets given a class active
. I need to show a component I made (to render) inside this particular class once the div has been clicked on. I am using next.js with react and having been using CSS modules.
Currently I tried: ReactDOM.render( <InnerDisc/>, document.getElementsByClassName(styles.active) );
but I get an error Error: Target container is not a DOM element.
Child (InnerDisc) Component
const InnerDisc = (props) => {
const active = props.active;
if (active) {
return (
<div className={styles.wrapper}>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
<Image className={styles.pic} src="https://picsum.photos/200/300" width={200} height={200}/>
</div>
);
}
}
export default InnerDisc;
Main App Component
export default function Home() {
const [discs, setDiscs] = useState([
{ id: 1, top: 100 },
{ id: 2, top: 200 },
{ id: 3, top: 300 },
{ id: 4, top: 400 },
{ id: 5, top: 500 },
{ id: 6, top: 600 },
{ id: 7, top: 700 },
{ id: 8, top: 800 },
{ id: 9, top: 900 },
{ id: 10, top: 1000 },
{ id: 11, top: 1100 },
{ id: 12, top: 1200 }
])
function enlargeDisc(e, num) {
let t = e.target
if (t.classList.contains(styles.active)) {
t.classList.remove(styles.active)
discRender()
} else {
t.classList.add(styles.active)
discRender()
}
}
function discRender() {
ReactDOM.render(
<InnerDisc/>, document.getElementsByClassName(styles.active)
);
}
return (
<div className={styles.container}>
<div className={styles.wrapper}>
{discs.map((item) => (
<div className ={styles.disc} key={item.id} style=.
{{top: item.top + 'px'}} onClick={(e)=>
enlargeDisc(e, item.id)}> </div>
))}
</div>
</div>
</div>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)