react node js code example
Example 1: command to create react app
npx create-react-app my-app
cd my-app
npm start
Example 2: create react app
yarn add create-react-app OR npm install create-react-app
npx create-react-app my-app
cd my-app
npm start
Example 3: build react app
Build a react project
Code has to be bundled using a bundler like webpack and
transformed using a compiler like Babel.
do production optimizations such as code splitting(critical CSS)
statically pre-render some pages for performance and SEO.
use server-side rendering or client-side rendering.
write some server-side code to connect your React app to your data store.
OR use create-react-app
Example 4: javascript react
class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = { items: [], text: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
render() {
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} />
<form onSubmit={this.handleSubmit}>
<label htmlFor="new-todo">
What needs to be done?
</label>
<input
id="new-todo"
onChange={this.handleChange}
value={this.state.text}
/>
<button>
Add
</button>
</form>
</div>
);
}
handleChange(e) {
this.setState({ text: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
if (this.state.text.length === 0) {
return;
}
const newItem = {
text: this.state.text,
id: Date.now()
};
this.setState(state => ({
items: state.items.concat(newItem),
text: ''
}));
}
}
class TodoList extends React.Component {
render() {
return (
<ul>
{this.props.items.map(item => (
<li key={item.id}>{item.text}</li>
))}
</ul>
);
}
}
ReactDOM.render(
<TodoApp />,
document.getElementById('todos-example')
);