22

I'm creating a simple crud in laravel and react js while im having a trouble with this error

app.js:21988 Warning: Invalid DOM property `for`. Did you mean `htmlFor`?
    in label (created by Add)
    in div (created by Add)
    in form (created by Add)
    in div (created by Add)
    in Add (created by Context.Consumer)
    in Route (created by Index)
    in div (created by Index)
    in Router (created by BrowserRouter)
    in BrowserRouter (created by Index)
    in Index (created by Context.Consumer)
    in Route (created by Header)
    in div (created by Header)
    in Router (created by BrowserRouter)
    in BrowserRouter (created by Header)
    in Header (created by Index)
    in div (created by Index)
    in div (created by Index)
    in div (created by Index)
    in div (created by Index)
    in div (created by Index)
    in Index

Here's my code

import React, { Component } from 'react';
import axios from 'axios';

export default class Add extends Component {

    constructor()
    {
        super();
        this.onChangeBlogsName = this.onChangeBlogsName.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.state={
            blogs_name:''

        }
    }
    onChangeBlogsName(e){
        this.setState({
            blogs_name:e.target.value
        });

    }

    onSubmit(e)
    {
        e.preventDefault();
        const blogs = {
            blogs_name : this.state.blogs_name

        }

        axios.post('http://127.0.0.1:8000/blogs/store' ,blogs)
        .then(res=>console.log(res.data));
    }

    render() {
        return (
            <div>
                <form onSubmit={this.onSubmit} >
                     <div className="form-group">
                        <label for="blogs_name">Title</label>
                        <input type="text" className="form-control" id="blogs_name" 
                        value={this.state.blogs_name} 
                        onChange={this.onChangeBlogsName} />
                    </div>
                     <button type="submit" className="btn btn-primary">Submit</button>
                </form>
            </div>
        );
    }
}
Ismael Padilla
  • 4,666
  • 4
  • 23
  • 33
Ledah Xille
  • 231
  • 1
  • 2
  • 4
  • Does this answer your question? [React ignores 'for' attribute of the label element](https://stackoverflow.com/questions/22752116/react-ignores-for-attribute-of-the-label-element) – Emile Bergeron Jan 27 '20 at 02:37

4 Answers4

61

When using React, you can't use the for keyword in JSX, since that's a javascript keyword (remember, JSX is javascript so words like for and class can't be used because they have some other special meaning!)

To circumvent this, React elements use htmlFor instead (see React docs for more information).

So, your render function should be (I only replaced for with htmlFor):

render() {
    return (
        <div>
            <form onSubmit={this.onSubmit} >
                <div className="form-group">
                    <label htmlFor="blogs_name">Title</label>
                    <input type="text" className="form-control" id="blogs_name" 
                        value={this.state.blogs_name} 
                    onChange={this.onChangeBlogsName} />
                </div>
                <button type="submit" className="btn btn-primary">Submit</button>
            </form>
        </div>
    );
}
Ismael Padilla
  • 4,666
  • 4
  • 23
  • 33
9

Replace for="" with htmlFor=""

In your case change this

<label for="blogs_name">Title</label>

To this

<label htmlFor="blogs_name">Title</label>
Abdulhakim Zeinu
  • 2,223
  • 23
  • 31
5

for is a reserved word in JavaScript this is why when it comes to HTML attributes in JSX you need to use something else, React team decided to use htmlFor respectively. You can check the list of attributes from here

Ronak Lalwani
  • 326
  • 2
  • 9
-4

Short answer, Remove

for="blogs_name"

In your case.

superup
  • 1,127
  • 11
  • 11