Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Unknown prop on <> tag. Remove this prop from the element

I'm new to react and i was learning subclasses from one of Lynda examples. I'm creating a new subcomponent class called aptList and using this.props.eachItem.ownerName to iterate through each index from the JSON file where ownerName is a property.

This is the error i get when i run it in the browser. The data gets fetched but the prop is not getting recognized according to the error

warning on console

however the react console seems to be getting the JSON fine

react console

This is the code as taught on Lynda

var React = require('react');
var ReactDOM = require('react-dom');
var createReactClass = require('create-react-class');

var aptList = createReactClass({
render: function(){
    return(
        <li>{ this.props.eachItem.ownerName }</li>
    );
}
});


var MainInterface = createReactClass({

   getInitialState: function(){
      return {
        title: 'Items',
        show: function(x){
            if(x>10){
                return true
            }
            else {
                return false
            }
        },
        myData: []
    }
},

componentDidMount: function(){

    this.serverRequest = $.getJSON('static/scripts/src/myData.json', function(results){
        var tempData = results;
        this.setState({
            myData: tempData
        });
    }.bind(this));
},


componentWillUnmount: function(){
    this.serverRequest.abort();
},

render: function(){

    var style = {
        color: 'red',
        fontWeight: 900
    };

    var reactData = this.state.myData;
    reactData = reactData.map(function (each, index) {
        return (
            <aptList eachItem = { each }
                     key = { index }/>
        )
    }.bind(this));

    return (
        <div>
            <h1>{ this.state.show(12) ? 'List of ':null }{ this.state.title }</h1>
            <ul style={style}>
                { reactData }
            </ul>
        </div>
        )
    }
});

ReactDOM.render(
  <MainInterface/>,
    document.getElementById('testid')
);
like image 790
Aswin Avatar asked Jun 27 '17 07:06

Aswin


People also ask

How do I turn off warnings in React?

The Yellow warning box in React Native can be both helpful and annoying. There are many errors that can be ignored but end up blocking necessary pieces of the application. To disable the yellow box place console. disableYellowBox = true; anywhere in your application.

How do you pass props in React JS?

To pass props, add them to the JSX, just like you would with HTML attributes. To read props, use the function Avatar({ person, size }) destructuring syntax.

What is shouldForwardProp?

shouldForwardProp accepts a prop and optionally the defaultValidatorFn , a function that is used by styled-components as the default function to determine if a prop should be passed.

How do you access props in a styled component?

Props can be passed to styled components just as they are with regular React components (class or functional). This is possible because styled components are actually React components. styled-components creates a React component, which renders an HTML tag corresponding to the property in the styled object.


1 Answers

Rename aptList to AptList.

Otherwise React considers aptList to be a native html component and will trigger warnings for unknown HTML properties.

See the link in the exception message:

  1. You are using a React component without an upper case. React interprets it as a DOM tag because ...
like image 112
Sulthan Avatar answered Sep 20 '22 23:09

Sulthan