score:1
Accepted answer
yes, it is possible using dangerouslysetinnerhtml
, but it is not recommended.
from the docs:
dangerouslysetinnerhtml is react’s replacement for using innerhtml in the browser dom. in general, setting html from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting (xss) attack. so, you can set html directly from react, but you have to type out dangerouslysetinnerhtml and pass an object with a __html key, to remind yourself that it’s dangerous.
here is a working example how this could be done:
const {usestate, useeffect} = react;
function app(){
const [data, setdata] = usestate("loading...");
useeffect(()=>{
const containerinnerhtml = document.queryselector('#element').innerhtml;
setdata(containerinnerhtml);
},[]);
return <div dangerouslysetinnerhtml={{__html: data}}/>;
}
reactdom.render(<app/>,document.getelementbyid('app'));
#element {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='app'></div>
<div id="element">
<div>node 1</div>
<div>node 2</div>
</div>
hope this helps!
Source: stackoverflow.com
Related Query
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS
- Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead
- React: Objects are not valid as a React child (found: object with keys. If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with key. If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {items}). If you meant to render a collection of children, use an array instead
- My error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- How to fix Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {})/If you meant to render a collection of children, use an array instead. Help me?
- Uncaught Error: Objects are not valid as a React child (found: object with keys {todo}). If you meant to render a collection of children, use an array
- Objects are not valid as a React child (found: object with keys {id, name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys} If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: [object Element]). If you meant to render a collection of children, use an array instead.- ReactJs
- Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. ReactJS
- react matrerial ui Error: Objects are not valid as a React child If you meant to render a collection of children, use an array instead
- ×Objects are not valid as a React child (found: object with keys {author, quote}). If you meant to render a collection of children, use an array
- Objects are not valid as a React child If you meant to render a collection of children, use an array instead
- REact error Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child . If you meant to render a collection of children, use an array instead
More Query from same tag
- Why this sort function is not returning to the original order?
- React won't render within map function
- Typescript inference for unions not picking up the correct type
- React this.props.match is undefined
- React Testing Library Invariant failed: You should not use <Route> outside a <Router>
- how to pass the data from data.json file in react components with the help of props in react js?
- React TS useContext and UseReducer with axios
- How can I keep font-size from parent to children on styled-components?
- How do i highlight a row based on another row condition in react jsx?
- upload zip file from reactjs to nodejs
- How to pass props using react router link v6?
- JSX return a html segment
- Webpack: postcss-loader is ignoring config file
- Cannot find module 'react-dev-utils/crossSpawn' exception React App
- How do I reference an inner class?
- React Application Environment Variable Undefined after Deploying
- react-select function call on click event on the chip
- React Router with arguments?
- ReferenceError: React is not defined in svg file
- Add Babel to Typescript React app
- Display 'No Rows' message after applying filter in MUI DataGrid
- Load CSS module in ReactJS + Typescript and react rewired
- react-hooks/exhaustive-deps causing dependency warning, fix hangs code
- AG-Grid React: Does Row Spanning work when using cellRenderer
- Set state method does appears to do nothing. Why?
- How to create multple fallback attributes via inline styles in React?
- Why did this reselect selector cause a re-render?
- Efficiently adding click listener to an arbitrary number of elements
- How to mock react custom hook return value as a module with Jest
- How to disable React Hook "useCallback" cannot be called inside a callback warning?