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
Resilient Oracle PL/SQL: Building Resilient Database Solutions for Continuous Operation
this week in the
JDBC and Relational Databases
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
Paul Clapham
Tim Cooke
Ron McLeod
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Junilu Lacar
Saloon Keepers:
Tim Holloway
Carey Brown
Stephan van Hulst
Peter Rooke
Mikalai Zaikin
Bartenders:
Himai Minh
Forum:
Code Reviews
Code review for my sample React application
Monica Shiralkar
Ranch Hand
Posts: 2773
13
posted 3 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
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
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...