3

I'm trying to pass a variable in return of a component. Here is my code:

const Obj = (props) => {

    let { propId } = useParams();
    
    const [data, setData] = useState({ course: [] });

    useEffect(() => {
        (async () => {
            const result = await axios.get(
                'http://example.com/api/v1/' + propId
            ).catch(err => {
                console.error(err);
            });
            setData(result.data);
        })();
    }, [propId]);
    
    return (
        <Fragment key={propId}>
            <div>
                {data.htmlContent}
            </div>
        </Fragment>
    );
};

export default Obj;

At here it shows this:

<p>Lorem ipsum dolor sit amet</p>

How can I insert this html content to main content?

Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
sundowatch
  • 3,012
  • 3
  • 38
  • 66

1 Answers1

6

Try the dangerouslySetInnerHTML attribute :

 <Fragment key={propId}>
            <div dangerouslySetInnerHTML={ { __html: data.htmlContent}} >
           
            </div>
        </Fragment>
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164