/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* @emails react-core
*/
'use strict';
import ButtonLink from 'components/ButtonLink';
import Container from 'components/Container';
import Flex from 'components/Flex';
import mountCodeExample from 'utils/mountCodeExample';
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import TitleAndMetaTags from 'components/TitleAndMetaTags';
import {colors, media, sharedStyles} from 'theme';
import createOgUrl from 'utils/createOgUrl';
import loadScript from 'utils/loadScript';
import {babelURL} from 'site-constants';
import ReactDOM from 'react-dom';
class Home extends Component {
componentDidMount() {
renderExamplePlaceholder('helloExample');
renderExamplePlaceholder('timerExample');
renderExamplePlaceholder('todoExample');
renderExamplePlaceholder('markdownExample');
function mountCodeExamples() {
mountCodeExample('helloExample', HELLO_COMPONENT);
mountCodeExample('timerExample', TIMER_COMPONENT);
mountCodeExample('todoExample', TODO_COMPONENT);
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
}
loadScript(babelURL).then(mountCodeExamples, error => {
console.error('Babel failed to load.');
mountCodeExamples();
});
}
render() {
const {data} = this.props;
const title = 'React - A JavaScript library for building user interfaces';
const marketingColumns = data.allMarkdownRemark.edges.map(edge => ({
title: edge.node.frontmatter.title,
content: edge.node.html,
}));
return (
React
A JavaScript library for building user interfaces
Get Started
Take the Tutorial
{marketingColumns.map((column, index) => (
))}
A Simple Component
React components implement a `render()` method that takes
input data and returns what to display. This example uses an
XML-like syntax called JSX. Input data that is passed into
the component can be accessed by `render()` via
`this.props`.
JSX is optional and not required to use React.
{' '}
Try the{' '}
Babel REPL
{' '}
to see the raw JavaScript code produced by the JSX
compilation step.
A Stateful Component
In addition to taking input data (accessed via
`this.props`), a component can maintain internal state data
(accessed via `this.state`). When a component's state data
changes, the rendered markup will be updated by re-invoking
`render()`.
An Application
Using `props` and `state`, we can put together a small Todo
application. This example uses `state` to track the current
list of items as well as the text that the user has entered.
Although event handlers appear to be rendered inline, they
will be collected and implemented using event delegation.
A Component Using External Plugins
React is flexible and provides hooks that allow you to
interface with other libraries and frameworks. This example
uses remarkable, an external Markdown
library, to convert the textarea's value in real time.
Get Started
Take the Tutorial
);
}
}
Home.propTypes = {
data: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
};
// TODO Improve this temporarily placeholder as part of
// converting the home page from markdown template to a Gatsby
// page (see issue #2)
function renderExamplePlaceholder(containerId) {
ReactDOM.render(
Loading code example...
,
document.getElementById(containerId),
);
}
const CtaItem = ({children, primary = false}) => (
{children}
);
// eslint-disable-next-line no-undef
export const pageQuery = graphql`
query MarketingMarkdown {
allMarkdownRemark(
filter: {id: {regex: "/marketing/"}}
sort: {fields: [frontmatter___order], order: ASC}
) {
edges {
node {
frontmatter {
title
}
html
}
}
}
}
`;
export default Home;
const sectionStyles = {
marginTop: 20,
marginBottom: 15,
[media.greaterThan('medium')]: {
marginTop: 60,
marginBottom: 65,
},
};
// TODO This nasty CSS is required because 'docs/index.md' defines hard-coded class names.
const markdownStyles = {
'& .example': {
marginTop: 40,
'&:first-child': {
marginTop: 0,
},
[media.greaterThan('xlarge')]: {
marginTop: 80,
},
},
};
// TODO Move these hard-coded examples into example files and out of the template?
// Alternately, move them into the markdown and transform them during build?
// This could be done via a new Gatsby transform plug-in that auto-converts to runnable REPLs?
const name = Math.random() > 0.5 ? 'John' : 'Jane';
const HELLO_COMPONENT = `
class HelloMessage extends React.Component {
render() {
return (
Hello {this.props.name}
);
}
}
ReactDOM.render(
,
mountNode
);
`.trim();
const TIMER_COMPONENT = `
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
tick() {
this.setState(prevState => ({
seconds: prevState.seconds + 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
Seconds: {this.state.seconds}
);
}
}
ReactDOM.render(, mountNode);
`.trim();
var TODO_COMPONENT = `
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 (
TODO
);
}
handleChange(e) {
this.setState({ text: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
if (!this.state.text.length) {
return;
}
const newItem = {
text: this.state.text,
id: Date.now()
};
this.setState(prevState => ({
items: prevState.items.concat(newItem),
text: ''
}));
}
}
class TodoList extends React.Component {
render() {
return (
{this.props.items.map(item => (
- {item.text}
))}
);
}
}
ReactDOM.render(, mountNode);
`.trim();
var MARKDOWN_COMPONENT = `
class MarkdownEditor extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = { value: 'Type some *markdown* here!' };
}
handleChange(e) {
this.setState({ value: e.target.value });
}
getRawMarkup() {
const md = new Remarkable();
return { __html: md.render(this.state.value) };
}
render() {
return (
);
}
}
ReactDOM.render(, mountNode);
`.trim();