Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>React -from- Scratch</h1>
<div id="container">
    <footer>
        <h3>What is this... "from scratch" thing?</h3>
        <p>This is a small project I try to write out from memory when possible.&nbsp;
           The Task: Write out all non-commented code in the JS code section from memory:</p>
        <p>The "enhanced" version of this project is at: <a href="https://codepen.io/KeithDC/pen/dzdqaV">https://codepen.io/KeithDC/pen/dzdqaV</a>&nbsp;
            (it has TypeScript.)</p>
        <hr/>
        <div id="last-time">
            My last time entry from <a href="https://codepen.io/KeithDC/pen/dVrwrg">my working version</a> <i>(which includes; Palindrome, FizzBuzz, Pomodoro, and GistBox (GetGist))</i>
            <pre>// 2018-03-26 (Mon) // Did all 4 (40 min)
                 // Started: 6:30 PM
                 // Finished: 7:10 PM</pre>
        </div>
        <hr/>
        <cite>
            This project was inspired by Laracasts' <a href="https://laracasts.com/series/do-you-react/episodes/7">"Do You React?" [episode 7]</a> (early 2017; although the code has been cleaned and kept up to at least Dec 2017)
        </cite>
        <code class="hidden"><cite>
            Times: [ 
                    {'2017-04-17', '(couldn&apos;t complete')'<span class="hidden">, comments: '11:20 - '</span>},
                    {'2017-04-18', '1.25 hrs'<span class="hidden">, comments: '10:40 -> 11:55 -- Got stuck on xhr - confused xhr.readyState with xhr.status. [readyState] is state of XHR request (waiting for DONE; 4), while [status] is HTTP response status (404, 304, 200).'</span>},
                    {'2017-04-19', '1.50 hrs'<span class="hidden">, comments: '1:45pm -> 3:15pm -> 3:40pm -- Got stuck on adding new gist into gistList array. Brought over into sublime and figured out 2 issues with gistList loop (tried forEach, but needs map for the return; couldn't use {} in arrow function).'</span>},
                    {'2017-04-20', '~25 min'<span class="hidden">, comments: '2:28pm -> 2:47pm -> 2:51pm -- Error #1: 'map' from undefined. Inital state was set as 'GistList'. Error #2: username not showing and url incorrect. Forgot to extrapolate the proper data from [gist.owner.login] and [gist.html_url]'</span>},
                    {'2017-04-21', '~20 min'<span class="hidden">, comments: '11:18am -> 11:37am -- As with yesterday, had to use Dev Tools to figure out a couple hiccups.'</span>},
                    {'2017-04-22', '~45 min'<span class="hidden">, comments: '2:48pm -> 3:18pm -- First time trying to write the new fetch() format in lieu of XMLHttpRequest. Problem came when trying to figure out why footer contents weren't showing up. I'd forgotten I'm now making a call to props.children to show that content. 3:38pm - Now works with new fetch()'</span>},
                    {'2017-04-23', '18 min'<span class="hidden">, comments: '6:29pm -> 6:47pm -- Wrote with fetch() method in lieu of XMLHttpRequest.'</span>},
                    {'2017-04-24', '20 min'<span class="hidden">, comments: '1:38 pm -> 1:57 pm -- works; 1:58 pm - added 'User has no gists' check and alert == 20 min - Got caught up for a few minutes because I didn't wrap my GistBox, GistList, nor my Gist return expressions in &lt;div&gt; tags. This is why I do this once daily.'</span>},
                    {'2017-04-25', '18 min'<span class="hidden">, comments: '11:58pm -> 12:16am'</span>},
                    {'2017-04-28', '17 min'<span class="hidden">, comments: '12:29pm -> 12:46pm -- Drew a blank on: response.json() -> went with .getJson() -> solved with a console.log on response in first .fetch() .then()'</span>},
                    {'2017-04-29', '16 min'<span class="hidden">, comments: '11:36am -> 11:52am -- A few blunders (Dev Tools console to the rescue); 1: mispelled 'proops' in constructor param - 2: Forgot &lt;form&gt; for submit - 3: User onClick instead of onChange - 4: Forgot 'this.' when calling updateUser with last gist - 5: Didn't add 'no gists' check -- Suggestion: Take it slower and be more mindful'</span>},
                    {'2017-04-30', '16 min'<span class="hidden">, comments: '6:16pm -> 6:32pm -- Worked; No assistance needed from dev tools'</span>},
                    {'2017-05-01', '12 min'<span class="hidden">, comments: '1:26pm -> 1:38pm -- Worked; One console.log output showed I missed wrapping the form for submit'</span>},
                    {'2017-05-03', '12 min'<span class="hidden">, comments: '4:28pm -> 4:40pm -- Worked; Although CodePen choked up when I pasted in the &lt;footer&gt; section'</span>},
                    {'2017-05-15', '32 min'<span class="hidden">, comments: '5:36pm -> 5:55pm -> 6:08pm -- Mostly just forgot syntax. Also forgot the fetch `response.json()`'</span>},
                    {'2017-05-16', '13 min'<span class="hidden">, comments: '6:26pm - 6:39pm -> 1 small miss - form input didn't clear (made input value [username] not [user])'</span>},
                    {'2017-05-17', '15 min'<span class="hidden">, comments: '6:29pm -> 6:39pm -> 6:44pm -- Forgot ReactDOM.render, and had to track down misspelled 'gettUser' prop callback.'</span>},
                    {'2017-05-23', '21 min'<span class="hidden">, comments: '7:14pm -> 7:28pm -> 7:35pm -- Missed ending } on form element; Missed renaming Gist to GistBox (copy/pasta); Mis-typed &apos;gist.length&apos; instead of &apos;data.length&apos; (from when I moved the if condition out of &apos;updateGistList&apos; method)'</span>},
                    {'2017-07-04', '22 min'<span class="hidden">, comments: '5:22pm -> 5:37pm -> 5:44pm -- Missed 'onSubmit' from 'Form onSubmit={this.submitUser}'; Missed 'return' from 'return response.json()'; Missed specifying parent 'gist' from 'gist.html_url'; Missed wrapping {braces} from this.setState({gistList}); When last issue was fixed, didn't even realize the "t" (username) was being displayed. Doh!; But I did do the entire fetch.'</span>},
                    {'2017-08-18', '23 min'<span class="hidden">, comments: '1:49pm -> 2:12pm'</span>},
                    {'2017-10-22', 'Training continued at: https://codepen.io/KeithDC/pen/dVrwrg'}
            ]
        </cite></code>
    </footer>
<!-- 
    DISPLAY
        FORM
            - Input - Text Field
            - Button - Submit ('Get Gist')
        LIST
            - Gists
    METHODS
        Input - handleUpdate (local component state)
        Button - handleSubmit (container callback; validate form, API call, post to source state)
    DATA
        Input (from user)
            - gist username
                - use username to get user gists
        Output (source state)
            [{
                - owner.login
                - html_url
            }]
-->
</div>
            
          
!
            
              body {
    font-family: arial;
    font-size: 14px;
}
.listitem {
    font-size: 12px;
}
footer {
    font-size: 11px;
    position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
}
cite {
    display: block;
    font-size: 10px;
    margin-bottom: .2rem;
}
.hide,
.hidden {
    display: none;
}
            
          
!
            
              // daily-project: GetGist

    // This is a 'from scratch' project I try to write out when possible
    // Note: I don't write out the 'dangerouslySetInnerHTML' part.

    // My new updated version is at: https://codepen.io/KeithDC/pen/dzdqaV
    // Although I would focus on this one first (there's no TypeScript).

    // This project was inspired by Laracasts (early 2017):
    // https://laracasts.com/series/do-you-react/episodes/7

    // Your Task: Write out everything below from memory:
    // --------------------------------------------------

// GistBox
    // GistForm
    // GistList
        // Gist

;console.clear();

const noGistMsg = 'This GitHub user has no Gists.';

class GistBox extends React.Component {

    state = {
        gistList: []
    };

    getGist = (usernameInput) => {
        const url = `https://api.github.com/users/${usernameInput}/gists`;

        async function fetchAsync(getUrl) {
            let response = await fetch(getUrl, { method: 'GET' });
            let data = await response.json();
            return data;
        };

        fetchAsync(url).then( data => {
            var thisGist = {};

            if (data.length > 0) {
                thisGist = data[0];
            } else {
                thisGist = {
                    owner: {
                        login: usernameInput
                    },
                    html_url: noGistMsg
                };
            }
            this.updateGistList(thisGist);
        })
    }
    updateGistList = (gist) => {
        let username = gist.owner.login,
            url = gist.html_url,
            gistList = this.state.gistList;
        gistList = gistList.concat({ username, url });
        this.setState({ gistList });
    }
    render() {
        return (
            <div>
                <GistForm submitUser={this.getGist} />
                <GistList gistList={this.state.gistList} />
                {this.props.children}
            </div>
        );
    }
}
class GistForm extends React.Component {

    state = {
        username: ''
    };

    formChange = (e) => {
        this.setState({ username: e.target.value });
    }
    formSubmit = (e) => {
        e.preventDefault();
        if (this.state.username.length > 0) {
            this.props.submitUser(this.state.username);
            this.setState({ username: '' });
        }
    }
    render() {
        return (
            <div>
                <form onSubmit={this.formSubmit}>
                    <label>Enter a GitHub username for a link to their last Gist:<br/>
                        <input 
                            type="text" 
                            onChange={this.formChange} 
                            value={this.state.username} 
                            placeholder="kdcinfo"
                            autoFocus={true}
                        />
                    </label>
                    <button>Get Gist</button>
                </form>
            </div>
        );
    }
}
class GistList extends React.Component {
    render() {
        const { gistList } = this.props;
        return (
            <table>
                <tbody>
                    { gistList.map( (gist, idx) => <Gist key={idx} gist={gist} /> ) }
                </tbody>
            </table>
        );
    }
}
class Gist extends React.Component {
    render() {
        const { username, url } = this.props.gist,
              userurl = (url === noGistMsg) ? noGistMsg : <a href={url} target="kdcNewWin">{url}</a>;
        return (
            <tr className="listitem">
                <td>{username}</td>
                <td>{userurl}</td>
            </tr>
        );
    }
}
ReactDOM.render(
    <GistBox>
        <footer dangerouslySetInnerHTML={{__html: document.querySelector('footer').innerHTML}}></footer>
    </GistBox>,
    container
)

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console