Swizec Teller - a geek with a hatswizec.com

    Intern process part 2 – (my first) webinar, about React context

    My first webinar! How I hate the word, but that's what it was.

    The plan was to get a bunch of prospective tech interns together on a stream, teach them a React concept, and give them something to build. Sunday night, I'm seeing what they build and figuring out who I want to work with.

    The goal?

    To judge people's slope vs. y-intercept. Are they fast learners, or do they just know a lot? I want to hire someone who learns fast, not someone who's already a pro. That way I'm working with a future pro and helping them get there.

    Imagine y is your skill and x is time. Slope always beats out y-intercept in the long run.

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    a little bit of slope makes up for a lot of y-intercept
    Tweet media

    What interns learned

    We learned about the new React context API and built a silly little app that shows off two benefits of context: 1. Shared state between components 2. No prop drilling

    Here’s the source on GitHub.

    We used React.createContext() to create a context:

    const LockContext = React.createContext()
    ```
    And a top level component to hold the state and callback we wanted to share. It's like a Redux or MobX store, you want your state and your callbacks to travel together.
    ```javascript
    class ContextualThing extends React.Component {
    state = {
    locked: false
    };
    toggleLock = () =>
    this.setState({
    locked: !this.state.locked
    });
    render() {
    const { locked } = this.state;
    return (
    <div>
    This is a lock
    <p style={{ fontsize: "2em" }}>
    {locked ? <b>Locked!</b> : "Open"}
    </p>
    <lockcontext class="provider" value={{ locked, togglelock: this.togglelock }}>
    {[1, 2, 3, 2, 1].map(n => <togglerow n={n}>)}
    </togglerow><table></table>
    </lockcontext>
    </div>
    );
    }
    }

    When state and callbacks go hand in hand, they're easier to manage. Helps you think in state machines.

    But I forgot to mention the state machine part in the webinar 😅

    Each ToggleRow renders as many table cells as you ask it to:

    const ToggleRow = ({ n }) => (
    <row>
    {new Array(n).fill(0).map((_) => (
    <cell>
    <locktoggle></locktoggle>
    </cell>
    ))}
    </row>
    );

    Notice the lack of prop drilling. <ToggleRow> gets only a number, n. Nothing about callbacks or the locked state. It doesn't even pass any props into <LockToggle>.

    That's because LockToggle is a smart context component 👇

    const LockToggle = () => (
    <lockcontext class="consumer">
    {({ locked, toggleLock }) => (
    <button onclick={toggleLock} style={{ fontsize: "1.5em" }}>
    {locked ? "Unlock" : "Lock"}
    </button>
    )}
    </lockcontext>
    );

    LockToggle renders a context consumer, which takes a function as children render prop. This render prop gets the necessary locked state and toggleLock callback in its arguments and returns a button that flips the lock.

    Et voilà: shared lock/toggle between components without prop drilling.

    I also answered a lot of questions about when you should use context versus Redux or MobX. Watch the stream 😉

    What Swiz learned

    Here's what I got out of the experience in a nutshell 1. Waiting for people to show up is nerve wracking 2. Lightly advertise in advance and people will show up 3. Start the stream on time. no need to be there the first 10 minutes 4. You can wing it if you know your shit 5. Good way to practice conference talks, I think 6. Maybe have some slides 7. Drawing ideas out on paper while explaining them is a great interactive way to produce slides in real time 8. The delay between what you say and what people hear is massive 9. Giving the audience time to ask questions is nerve wracking

    What's next

    More webinars I think. Great way to develop new content and a low friction approach to video production. That gives me ideas. 🤔

    Plus, answering questions from the audience is a great way to see what you forgot to cover. That's slope right there!

    Did you enjoy this article?

    Published on June 14th, 2018 in Business, Front End, Technical

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️