Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Code Reviews
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Code Reviews
Code review for my sample React application
Monica Shiralkar
Ranch Hand
Posts: 2953
13
posted 4 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have implemented a sample react application for adding Todos. Can someone please review my code?
Index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> <!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> <title>React App</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="mytodos"></div> <!-- This HTML file is a template. If you open it directly in the browser, you will see an empty page. You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the <body> tag. To begin the development, run `npm start` or `yarn start`. To create a production bundle, use `npm run build` or `yarn build`. --> </body> </html>
MyTodos.js
import React from 'react'; import ReactDOM from 'react-dom'; import logo from './logo.svg'; import './App.css'; class MyTodoList extends React.Component { constructor(props) { super(props); this.state = {value: '',itemsList: []}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { console.log("event.target.value"+event.target.value); this.setState({value: event.target.value}); // this.setState({value: this.state.itemsList.concat( event.target.value)}); } handleSubmit(event) { console.log("this.state.value"+this.state.value); this.setState({itemsList: this.state.itemsList.concat( this.state.value)}); event.preventDefault(); } render() { return ( <form onSubmit={this.handleSubmit}> <h1>My Todo section</h1> <label> Name: <input type="text" value={this.state.value} onChange={this.handleChange} /> </label> <input type="submit" value="Submit" /> <MyTodos data={this.state}/> </form> ); } } class MyTodos extends React.Component { render() { console.log("hello"); //console.log(" state retrieved -> "+ this.state.itemsList) ; var itemsList= this.props.data.itemsList; var itemsListHtmlContent= " "; if(this.props.data) //debugger; console.log("this.props.data.itemsList->"+ this.props.data.itemsList); console.log("itemsListt->"+ itemsList); //console.log("this.props.itemsList->"+ this.props.itemsList); for (var i = 0; i < itemsList.length; i++) { itemsListHtmlContent=itemsListHtmlContent.concat(itemsList[i]+" "+'<br /> '); } console.log("itemsListHtmlContent"+itemsListHtmlContent); console.log("itemsListHtmlContent"+itemsListHtmlContent); return ( <div> <h1> MyTodo List</h1> {itemsListHtmlContent} </div> ); } } ReactDOM.render( <MyTodoList />, document.getElementById('mytodos') );
Thanks
It's feeding time! Give me the food you were going to give to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Populating React dropdown list with data from axios request
Object being added as a empty string
React State
Edit function in react
How can I place 3 markers with user input instead of 1 on Google Maps?
More...