I have two react components.
- List container
- List
The list needs to be inside the list container. Like so:
JavaScript
x
2
1
<Container innerhtml={<List></List>} ></Container>
2
The content of both components renders. However the styling of the child is overridden by the parents styling. (In this case, the background color)
This is the full code:
JavaScript
1
27
27
1
import React from "react";
2
3
export default function main() {
4
return <div>
5
6
<Container
7
innerhtml={<List></List>}
8
></Container>
9
10
</div>
11
}
12
13
function List() {
14
15
return <div style={{ backgroundColor: "#red!important", height: "150px", width: "150px" }}>
16
this is a list
17
</div>
18
}
19
20
function Container(props) {
21
22
return <div style={{ backgroundColor: "#94e49d38", height: "400px", width: "100vw-10px" }}>
23
this is container
24
{props.innerhtml}
25
</div>
26
}
27
I think it may be a similar thing to this: Style not working for innerHTML in Angular
However I cant find a React equivalent.
How can I get the list style to work?
Thank you
Advertisement
Answer
By refactoring a bit your code I found this Solution:
JavaScript
1
32
32
1
export default function Main() {
2
return (
3
<div>
4
<Container>
5
<List></List>
6
</Container>
7
</div>
8
);
9
}
10
11
function List() {
12
return (
13
<div style={{ backgroundColor: "red", height: "150px", width: "150px" }}>
14
this is a list
15
</div>
16
);
17
}
18
19
function Container(props) {
20
return (
21
<div
22
style={{
23
backgroundColor: "#94e49d38",
24
height: "400px",
25
width: "100vw-10px"
26
}}
27
>
28
{props.children}
29
</div>
30
);
31
}
32
by passing props.children
instead of innerHtml
and by removing the “#” before red this works fine, see the sandbox