DEV Community

Cover image for DIY React: Because Who Needs Stability Anyway?
Prayush Adhikari
Prayush Adhikari

Posted on

DIY React: Because Who Needs Stability Anyway?

Welcome back, intrepid developers, to another thrilling episode number two of "React Unmasked"! Or should I say, "We Have React at Home"? 🏠🎭 Today, we're going to embark on a journey that's about as advisable as trying to build your own nuclear reactor in your backyard. That's right, we're creating our own React!

Why Build Your Own React?

You might be wondering, "Why on earth would I want to build my own React?" Well, dear reader, for the same reason people climb Mount Everest or eat ghost peppers – because it's there, and we're probably not thinking this through entirely.

The "We Have React at Home" Renderer

Behold, the pinnacle of human achievement:

function customRender(reactElement, container) {
    const domElement = document.createElement(reactElement.type)
    domElement.innerHTML = reactElement.children
    for (prop in reactElement.props) {
        if (prop === 'children') continue;
        domElement.setAttribute(prop, reactElement.props[prop])
    }
    container.appendChild(domElement)
}
Enter fullscreen mode Exit fullscreen mode

This function is like React, but on a budget. It renders stuff. Sometimes. πŸ€·β€β™‚οΈ

Let's break it down:

  1. We create a DOM element. It's like giving birth, but to HTML.
  2. We set the inner HTML. Hope you sanitized that input! 🧼
  3. We iterate through props like it's Black Friday and props are on sale.
  4. We skip the 'children' prop. We don't do child labor here. πŸ‘ΆβŒ
  5. We set attributes. It's like accessorizing, but for elements.
  6. Finally, we append the element to the container. It's moving day! πŸ“¦

Creating Our "React" Element

Now, let's create our hand-crafted React element. It's organic, free-range, and gluten-free:

const reactElement = {
    type: 'a',
    props: {
        href: 'https://github.com/adhikareeprayush',
        target: '_blank'
    },
    children: 'My Github'
}
Enter fullscreen mode Exit fullscreen mode

Rendering Our Masterpiece

Time to bring our creation to life:

const main = document.querySelector('#root')
customRender(reactElement, main)
Enter fullscreen mode Exit fullscreen mode

And voilΓ ! We've just rendered a link using our home-brewed React. Michelangelo would be proud. Or horrified. Probably horrified.

Why This Is (Probably) a Terrible Idea

  1. Performance: This renderer is about as optimized as a sloth on sedatives.
  2. Features: We're missing about 99.9% of React's features. But who needs those, right?
  3. Security: XSS vulnerabilities? Never heard of her.
  4. Maintenance: Good luck updating this when the next JavaScript framework drops in about 5 minutes.

Conclusion

So there you have it, folks! We've created our own React at home. Is it good? No. Is it React? Also no. But did we learn something? Maybe, and isn't that the real React we built along the way?

Remember, just because you can build your own React, doesn't mean you should. Leave it to the professionals, or at least to the caffeinated developers at Facebook.

Stay tuned for our next episode: "We Have GraphQL at Home: A String and a Lot of Wishful Thinking"!

Check Out the Real Deal

If you want to see this masterpiece in action (and why wouldn't you?), check out the REACT-AT-HOME GitHub repository. Just don't tell the React team what we're up to. They might get ideas.

And don't forget to subscribe to PrayushDev for more "Is this legal?" coding adventures!

Happy coding, and remember: with great power comes great responsibility. Use this knowledge wisely. Or don't. I'm a blog post, not a cop.

Join the PrayushDev Community!

Before you go, I'm excited to announce the launch of my weekly newsletter: PrayushDev! πŸŽ‰

Why should you subscribe? Here's what you'll get:

  • Bite-sized introductions to hot tech topics
  • Insider tips and tricks from the world of development
  • Early access to upcoming "React Unmasked" episodes
  • A chance to be part of a growing community of passionate devs

Don't miss out on this opportunity to stay at the cutting edge of tech. Subscribe now and let's grow together!

Subscribe to PrayushDev

Support My Journey as a Microsoft Student Ambassador

I'm on a mission to make a difference in the tech community, and I need your help! I'm currently working towards a goal as a Microsoft Student Ambassador, and your support would mean the world to me.

Here's how you can help:

  1. Visit my special Microsoft Student Ambassador link: Click Here
  2. Explore the page and learn about the amazing initiatives
  3. Share the link with your friends and colleagues

My goal is to reach 250 visitors, and every click counts! By supporting this initiative, you're not just helping me – you're contributing to a broader movement of tech education and community building.

Thank you for being part of this journey. Together, we can make a real impact in the world of technology!

Top comments (1)

Collapse
 
yunesh_shrestha_92d55c598 profile image
Yunesh Shrestha

That's a great content. Never thought of building my own react. Keep writing blogs!