2

I have a JSON string created by API:

var appStructure = {
    "code": 200,
    "message": "Register Form Loaded.",
    "response": "'div',{},'Registration'"
};

I want to create element dynamically using the API response. I am trying to do something like this:

var testCard1 = React.createElement(appStructure.response);
ReactDOM.render(testCard1, document.getElementById('main-content'));

But it keep giving me the error

Warning: is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements. Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('h1',{},'Hello Testing') is not a valid name.

1 Answer 1

2

appStructure.response is string and can't be passed into createElement method as arguments. You should change string to acceptable arguments. Also testCard1 should be in PascalCase: TestCard1

var args = appStructure.response.split(',');
var TestCard1= React.createElement(args[0].replace(/\W/g, ''), JSON.parse(args[1]), args[2].replace(/\W/g, ''));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I have another issue, i am creating form and inputes using api response but form tag is closed just after open, i want that form tag should close at the last input button. I am using React.createElement in loop and applying this state on all inputes coming, so everything is working fine but form tag is closed at the time of open and other inputes are placed outside that form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.