02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is SASS.

Removing Unknown Props from HTML

Handling props in React efficiently is crucial for ensuring smooth functionality and avoiding unnecessary errors in the browser console. In this tutorial, you’ll learn how to filter props to avoid passing invalid attributes to HTML elements, making your React components cleaner and error-free.


Why Filtering Props Matters

When building React components, developers often pass props for configuration or logic. However, some of these props might not correspond to valid HTML attributes. Passing such props to HTML elements leads to console errors and potential unintended behavior. By filtering these props, we ensure compliance with HTML standards and maintain clean code.


Step 1: Identifying the Problem

Scenario

Imagine a Jumbotron component receiving a prop like containerFluid. This prop is vital for internal logic but is not recognized as a valid HTML attribute for a <div> element.

The Error

React warns you in the console:
Warning: Unknown prop 'containerFluid' on <div> tag.

To solve this, we need to strip out non-HTML props before passing them to native elements.


Step 2: Duplicating and Filtering Props

  1. Start with a Copy of Props
    Use Object.assign to create a copy of all props received by the component:

    const props = Object.assign({}, this.props);
    
  2. Filter Out Unnecessary Props
    Delete any props that aren’t meant for the HTML element. For example:

    delete props.containerFluid;
    
  3. Pass Only Valid Props
    Use the filtered props object when rendering your HTML element:

    <div {...props} className="jumbotron">
        {this.props.children}
    </div>
    

Step 3: Example Implementation

Modified Jumbotron Component

class Jumbotron extends React.Component {
    render() {
        const props = Object.assign({}, this.props);
        const fluid = props.containerFluid; // Extract the custom prop
        delete props.containerFluid; // Remove it from the props object
        
        return (
            <div {...props} className={`jumbotron ${fluid ? 'jumbotron-fluid' : ''}`}>
                {this.props.children}
            </div>
        );
    }
}

Why This Works

  • The Object.assign method creates a shallow copy of the props, leaving the original untouched.
  • Deleting containerFluid ensures it doesn’t appear as an attribute in the final rendered HTML.
  • The fluid variable is used internally to determine the appropriate class name.

Step 4: Test the Solution

  1. Add a containerFluid prop to your Jumbotron component in the app:

    <Jumbotron containerFluid={true}>
        <h1>Welcome</h1>
    </Jumbotron>
    
  2. Inspect the element in the browser:

    • Verify that the <div> has the correct class (jumbotron jumbotron-fluid).
    • Confirm there is no containerFluid attribute in the rendered HTML.
  3. Check the console for errors — there should be none.


Next Steps

This technique ensures your React components handle props cleanly, keeping your application error-free. In the next tutorial, we’ll build on this concept by dynamically handling child components within our Jumbotron. This will further enhance its reusability and dynamic capabilities.

Video Transcript

00:00React components could get a lot of different properties
00:03and many of them are not valid HTML properties.
00:06So what happens and how do we only sub send a subset
00:09of those properties to HTML elements
00:12to avoid seeing errors in our error panel?
00:15And that's exactly what we're gonna do in this lecture.
00:20All right, so let's go ahead
00:21and make things even more dynamic.
00:23I'm in the app JS and I'm going to my jumbotron.
00:27And what I wanna do is I wanna basically declare another
00:31variable that is not built in to HTML, such
00:34as container fluid.
00:38And I want to set its value to be true, just
00:41to set here a variable that will change things around.
00:44Now if you run your code though,
00:46things will continue running.
00:47If you check in your console, you'll see
00:49that you're getting an error
00:51and it's basically telling you that this is not a variable
00:54that should be defined in a diviv
00:55because it's not illegal HTML parameter
00:58or attribute more accurately.
01:01Well, let's go ahead and let's go ahead
01:03and solve this problem.
01:05And it's quite easy and really cool.
01:07So first of all, I'm gonna go ahead here
01:09and what I'm gonna do is I'm gonna create a new constant
01:12and I'm gonna go to the constant and call it props.
01:15And what I'm gonna do is I'm gonna use a ES six method
01:19of the object, which is called assign.
01:21And I'm gonna say, Hey, I want you
01:22to assign into this new object object
01:25that I'm creating all the properties that are innumerable
01:28inside of this stop props.
01:30Now that I have a props that is disconnected, I could first
01:34of all go ahead here and set the,
01:35instead of this stop props.
01:37We don't need this anymore.
01:38Further than that, I could go ahead
01:41and also declare another set up here such as container fluid
01:46and go to my props container fluid.
01:52And I could also go to my container
01:54and set my fluid, fluid, fluid value
01:59to be the value inside of container fluid.
02:02Last but not least, I'll go ahead
02:04and also delete that variable by going into my props,
02:09props, container fluid and deleting it that way.
02:12Once we're getting into the divs assignment here,
02:15it's not going to trigger an error.
02:17And if anyone puts in a variable that we're not expecting,
02:20then they will get an error,
02:21which is more than perfectly fine with us.
02:24Notice how we're deleting the props.
02:26We're not deleting this variable because we need it.
02:29And OSU is not a variable that it's a constant.
02:31So we can't delete it.
02:32But we could delete a property of something that is constant
02:36'cause this object itself, we could still access it
02:38and manipulate it.
02:40With that said, let's go ahead and test this out.
02:42So now that we're sending in our fluidity variable through,
02:45if we go ahead and test our element and go
02:49and inspect it, we should find inside
02:51of our jumbotron a container.
02:54Okay, so that's a good start, but it's not fluid.
02:56So let's double, triple, quadruple check fluid.
03:00And you, let me just go ahead here
03:01and do here a console log
03:06for container fluid.
03:09And by the way, uh, a good, a good, um, shorter name.
03:12We could could just call it fluid once we were inside,
03:15but outside we needed to say container
03:16'cause we're not defining the parent.
03:19But if I go ahead here and just test it out to see
03:22what is the value that's coming through,
03:23is the value coming through at all?
03:26And I don't see it. I see your undefined.
03:29Okay, so unknown, unknown prop container fluid on diviv.
03:33So we, we didn't actually delete it
03:35and we're also getting undefined,
03:37so we're not actually accessing
03:38what we were hoping to access.
03:40So if I go ahead here
03:41and I just check this container fluid, let me just save
03:46that, go into my jumbotron, make sure
03:50that I'm naming the variables correctly.
03:53'cause I, sometimes I don't know why I do that,
03:56but I am dyslexic.
03:57So fluid, so it's not fluid.
04:00And if we take another peak, hopefully now,
04:03if everything went according to plan, we should now see
04:07inside here our fluid item.
04:09Beautiful, which is exactly what we were hoping
04:13to happen in this lecture.
04:17We learned how to avoid the errors
04:19that we would get if we just pass our, this,
04:21that props directly to an HTML element.
04:24Instead of that, we saw how we could duplicate our object
04:27and only send whatever we actually need to out of it.
04:30In the next lecture, we're gonna go ahead
04:32and complete our jumbotron while we create a dynamic
04:36fluid child.
04:38I.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!