npm install -g create-react-app
Create a project named react-chat
create-react-app react-chat
Run a project named react-chat
cd react-chat
yarn start
# or
npm start
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<img src={logo} className='App-logo' alt='logo' />
Welcome to React
To get started, edit <code>src/App.js</code> and save to reload.
);
}
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
public/
src/
App.css
App.js
App.test.js
index.css
index.js
logo.svg
registerServiceWorker.js
const ComponentName = () => {
return (
Some text
);
};
class ComponentName extends React.Component {
render() {
return (
Some text
);
}
}
React.createElement('div', {a: 1}, 'Some text')
class Title extends React.Component {
render() {
return (
Some title
);
}
}
Some title
class Header extends React.Component {
render() {
return (
<Title />
);
}
}
Some Text
import './App.css';
const Message = () => {
return (
// insert code here
);
};
class App extends Component {
const Message = () => {
return (
Some text
);
};
class App extends Component {
render() {
return (
{/* previous code */ }
{/* insert code here */ }
);
}
}
class App extends Component {
render() {
return (
{/* previous code */ }
<Message />
);
}
}
import React from 'react'; // node_modules
import App from './App'; // project
export default App;
src/
App.js
...
Message.js
import React from 'react';
const Message = () => {
// existing code
};
export default Message;
import React, { Component } from 'react';
import Message from './Message'
import logo from './logo.svg';
class App extends Component {
<Title text='The Cat' isMedium={true} />
React.createElement(Title, {text: 'The Cat', isMedium: true});
const Title = ({text, isMedium}) => {
const sizeText = isMedium ? '(Medium)' : '(Not Medium)';
return (
{text} {sizeText}
);
}
const Title = ({text, isMedium}) => {
const Title = (props) => {
const text = props.text;
const isMedium = props.isMedium;
const Message = (/* insert code here */) => {
return (
{/* insert code here */}
);
};
const Message = ({text}) => {
return (
{text}
);
};
class App extends Component {
render() {
return (
<Message /* insert code here */ />
);
}
}
class App extends Component {
render() {
return (
<Message text='Some bit of text' />
);
}
}
const Message = ({text, /* insert code here */}) => {
return (
{/* insert timestamp code here */}
{/* insert name code here */}
{text}
);
};
const Message = ({name, text, timestamp}) => {
return (
{timestamp}
{name}
{text}
);
};
class App extends Component {
render() {
return (
<Message text='Some bit of text' /* insert code here */ />
);
}
}
class App extends Component {
render() {
return (
<Message name='Rando' text='Some bit of text' timestamp='11:10pm'/>
);
}
}
npm install --save prop-types
import React from 'react';
// insert code here
const Message = ({name, text, timestamp}) => {
// existing code
}
Message.propTypes = {
name: // insert code here
text: // insert code here
timestamp: // insert code here
};
import React from 'react';
import PropTypes from 'prop-types';
const Message = ({name, text, timestamp}) => {
// existing code
}
Message.propTypes = {
name: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
timestamp: PropTypes.string.isRequired,
};
Message.propTypes = {
name: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
timestamp: PropTypes.instanceOf(Date).isRequired,
};
class App extends Component {
render() {
return (
<Message name='Rando' text='Some bit of text' timestamp={new Date()} />
);
}
}
const Message = ({name, text, timestamp}) => {
const time = timestamp.toTimeString();
return (
{/* existing code */}
{time}
{/* existing code */}
)
}
class App extends Component {
render() {
return (
{/* existing code */}
<Message // existing code />
<Message // insert code here />
);
}
}
class App extends Component {
render() {
return (
{/* existing code */}
<Message // existing code />
<Message text='A message with no name' timestamp={new Date()} />
);
}
}
import React from 'react';
// existing code
Message.propTypes = // existing code
Message.defaultProps = {
name: // insert code here
};
import React from 'react';
// existing code
Message.propTypes = // existing code
Message.defaultProps = {
name: 'Anonymous'
};
class Title extends React.Component {
render() {
const style = {fontSize: '20rem', padding: 10};
return (
Some text
);
}
}
class Title extends React.Component {
render() {
return (
Some text
);
}
}
const styles = {
h1: {
fontSize: '2rem',
padding: 10,
},
};
const Message = ({name, text, timestamp}) => {
// existing code
return (
{/* existing code */}
<span /* insert code here */ >
{name}
</span>
{/* existing code */}
);
};
const styles = {
// insert code here
};
const Message = ({compact, name, text, timestamp}) => {
const time = timestamp.toTimeString().split(' ')[0];
return (
{name}
{time}
{text}
);
};
const styles = {
name: {
fontWeight: 'bold',
},
text: {
marginTop: 0,
},
time: {
color: 'grey',
fontSize: '.8rem',
marginRight: '1rem',
},
};
class App extends Component {
render() {
return (
{/* existing code */}
<Message // existing code />
<Message // existing code />
);
}
}
const styles = {
messages: {
width: '50%',
margin: '0 auto',
},
};
// input
[1, 3, 5, 7]
// output
[10, 30, 50, 70]
function multiplyBy10(numbers) {
// insert code here
}
// example
const testArray = [1, 3, 5, 7];
const resultArray = multiplyBy10(testArray);
console.log(testArray); // prints [1, 3, 5, 7];
console.log(resultArray); // prints [10, 30, 50, 70]
function multiplyBy10(numbers) {
return numbers.map(number => 10 * number);
}
function multiplyBy10(numbers) {
return numbers.map(function (number) { return 10 * number; });
}
render() {
return (
Some text
{someFunction()}
);
}
render() {
return (
<Character name='Arya' />
<Character name='Jon' />
<Character name='Dany' />
);
}
render() {
const profiles = [{name: 'Arya'}, {name: 'Jon'}, {name: 'Dany'}];
return (
{profiles.map(profile =>
<Character name={profile.name} />
)}
);
}
const MessagesList = (/* insert code here */) => {
return (
// insert code here
);
};
class MessagesList extends React.Component {
render() {
const {messages} = this.props;
return (
{messages.map(message =>
<Message key={message.timestamp.toString() + message.name} avatarUrl={message.avatarUrl} name={message.name} timestamp={message.timestamp} text={message.text} />
)}
);
}
}
import React from 'react';
// insert code here
const messages = [
{/* insert message content */},
{/* insert message content */},
];
class App extends Component {
render() {
return (
{/* insert code here */}
);
}
}
import MessagesList from './MessagesList';
const messages = [
{name: 'A', timestamp: new Date(2017, 9, 1), text: 'Hello'},
{name: 'B', timestamp: new Date(), text: 'Hi'},
{name: 'C', timestamp: new Date(), text: 'Howdy'},
];
class App extends Component {
render() {
return (
<MessagesList messages={messages} />
);
}
}
const message = loggedIn ? 'Welcome' : 'Please log in';
render() {
return (
{loggedIn ? <App /> : <LoginScreen />}
);
}
render() {
return (
{loggedIn &&
<Profile />
}
);
}
const Message = ({name, text, timestamp, /* insert code here */}) => {
return (
{/* existing code */}
<p style={/* insert code here */}>
{text}
</p>
);
}
const styles = {
// existing code
textCompact: {
display: 'inline',
marginLeft: '1rem',
},
};
const Message = ({name, text, timestamp, compact}) => {
return (
<div style={compact ? styles.textCompact : styles.text}>
{name}
</div>
);
};
const styles = {
// existing code
textCompact: {
display: 'inline',
marginLeft: '1rem',
},
};
class Dog extends React.Component {
constructor(props) {
super(props);
this.state = {isBarking: false};
}
}
class App extends React.Component {
constructor(props) {
// insert code here
}
// existing code
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {compact: false};
}
// existing code
}
class App extends React.Component {
// existing code
render() {
// insert code here
return (
<MessagesList /* insert code here */ messages={messages} />
);
}
}
class App extends React.Component {
// existing code
render() {
const {compact} = this.state;
return (
<MessagesList compact={compact} messages={messages} />
);
}
}
const MessagesList = ({messages, /* insert code here */}) => {
return (
{messages.map(({name, text, timestamp}, index) =>
<Message
key={index + timestamp.getTime()}
/* insert code here */
name={name}
text={text}
timestamp={timestamp}
/>
)}
);
};
const MessagesList = ({messages, compact}) => {
return (
{messages.map(({name, text, timestamp}, index) =>
<Message
key={index + timestamp.getTime()}
compact={compact}
name={name}
text={text}
timestamp={timestamp}
/>
)}
);
};