score:1
Accepted answer
const MyComponent = ({
title
}) => (
<div>{title}</div>
)
const MyDoc = () => (
<Code>
<MyComponent title="My title" obj={{obj: {obj: 1}}}>
<MyComponent title="My another component title">
<MyComponent title="My another component title" />
</MyComponent>
</MyComponent>
</Code>
)
const Code = ({
children
}) => (
<div>
{children}
<pre><code>
{JsxString(children)}
</code></pre>
</div>
)
const JsxString = (component, counter = 0) => {
let type = component.type.name;
let props = component.props;
let propsString = "";
for (let key in props) {
if (key !== "children") {
let propValue = props[key];
let value = "";
if (propValue instanceof Object) {
value = `{${JSON.stringify(propValue).replace(/['"]+/g, '')}}`;
} else {
value = `"${propValue}"`;
}
propsString += ` ${key}=${value}`;
}
}
if (props.children) {
counter += 2;
var children = JsxString(props.children, counter);
return `<${type}${propsString}>
${Array(counter).join(" ")} ${children}
${Array(counter).join(" ")}</${type}>`;
}
return `<${type}${propsString} />`;
}
ReactDOM.render(
<MyDoc />,
document.getElementById('container')
);
Source: stackoverflow.com
Related Query
- React Hooks - How to get parameter value from query string
- How to get children type in react
- How do I render a string as children in a React component?
- How can I get data from query string using route in react js
- How to get React children from Enzyme
- How to get multiple children to set one parent state without overwriting each other react js
- React - How to get a form to communicate with its children (form inputs)
- React + TS: How to get all children heights?
- How can i get children props value outside react map
- How can I get multiple string parameters in React Router
- React Chatbox, how to get the string displayed?
- How to get ref on children using react hook?
- React Router v4 - How to get current route?
- How to get rid of underline for Link component of React Router?
- How to get the width of a react element
- How to use children with React Stateless Functional Component in TypeScript?
- How to render a multi-line text string in React
- How to get a react component's size (height/width) before render?
- How do I restrict the type of React Children in TypeScript, using the newly added support in TypeScript 2.3?
- How to query by text string which contains html tags using React Testing Library?
- GET with query string with Fetch in React Native
- How to get "key" prop from React element (on change)?
- How to get "key" prop from React element (on change)?
- React - how to capture only parent's onClick event and not children
- How can I get input radio elements to horizontally align in react [material-ui]?
- How to get query string using React?
- How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?
- Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
- How to get the data from React Context Consumer outside the render
- How to check how many React children have a certain prop?
More Query from same tag
- Destructuring state/props in React
- redux mapDispatchToProps not updating state
- Save local state in React
- props onChange is not triggering
- React - Uncaught ReferenceError: add is not defined
- slideUp() and slideDown() animation using React.js
- How to edit row name while using ag-grid react
- React Router 'Home is not defined'
- React - Need help converting path to string so users get redirected to Stripe when clicking img
- Best way to use Mongoose to update an Array of Objects?
- How to dynamically add scripts received from CMS to <Head>
- Passing a function as a prop, when calling the function it doesn't get the correct values
- How to use reduce method by loadash for nested/complex object
- onClick like button likes all objects in array React JS
- Pass state change from onChange event to parent
- How to map within a map in React
- Use typescript to limit payload type with action name
- Fetching Array of Arrays from API ReactJs
- Filter fetch data react
- How to preserve the state for a "just hidden" component
- Material-UI List side by side React
- Firebase hosting with react router
- New to react. IM having issues with nav bar. Js code is not responding
- Prevent an onClick event on a span element when it was clicked once
- React.js implement menu [highlight active link]
- ReactJS Using styles within classComponent
- Need to add a simple functionality to a simple calculator made in React JS
- Reactjs - accessing variables
- How to push values correctly into an array of objects
- React Redux Login issue