react icon indicating copy to clipboard operation
react copied to clipboard

how to define, create, assign, use, engineer or otherwise use either a props property or a state to a class component button

Open dcu-sharepoint opened this issue 9 years ago • 5 comments

Do you want to request a feature or report a bug? how about an answer....

What is the current behavior?

40,0000 foot high level of frustration not relieved by reading over 100 different articles, doc articles, 4 pluralsight courses, stack overflow answers and many other useless leads.

**code provided

class firstComponent extends Component{
    render(){
        return(
            <div>
                <h1>my first component</h1>
                <Button />
            </div>
        )
    }
}
export default firstComponent;

class Button extends Component{
    constructor(props){
        super(props);
        console.log('handleClick');
       this.state = {counter: 25};

    }
    handleClick(e){
        e.preventDefault();
        console.log('Button:handleClick');
        this.state = {counter: 10};         THIS DOESN'T WORK '
    }
    render(){
        // console.log(this.state.counter);
        var counter = 15;
        return (
            <div>
            <h1>Button: component</h1>
            <h1>{this.props.counter}</h1>
            <button onClick={this.handleClick}>{this.state.counter}</button> this crap doesn't work either.'

            <h1>{this.props.counter}</h1>
            </div>
        )
    }
}

export default Button;

What is the expected behavior? Something that works as good a plain HTML (maybe that's what I should be using. **React v15, chrome browser, edge, FF / Windows 10

This works in previous versions of React using the var foo = React.createClass({})** but have not been able to get to work with the new class version and I can't find ANY examples that work and have been trying for well over a week. I need to start migrating some react modules from v0.13 to v15 but this is telling me I should give up and move on...

React may be fast but it's a PITA to start with and to update your knowledge and the glorified DOCS don't help

dcu-sharepoint avatar Mar 11 '17 03:03 dcu-sharepoint

The docs on state show how to use it: by calling setState. Not by reassigning this.state.

For example:

    handleClick(e){
        e.preventDefault();
        console.log('Button:handleClick');
        this.setState({counter: 10});
    }

Additionally, due to the way this works in JavaScript, you need to bind any event handlers in the constructor:

    constructor(props){
        super(props);
        this.state = {counter: 25};
        this.handleClick = this.handleClick.bind(this);
    }

It is also described in the documentation.

I hope this helps!

gaearon avatar Mar 11 '17 04:03 gaearon

@gaearon Thanks for your prompt answer. as stated at the beginning of the question: 40,0000 foot high level of frustration----- I've read the doc several times and the light bulb refused to be turned on. I did get it once I read some more articles and re-read the docs and I've been able to create my first app...

it's been painful, though but I like what I see.

regarding your example:

constructor(props){ super(props); this.state = {counter: 25}; this.handleClick = this.handleClick.bind(this); }

?1 why do I need to have props argument in the constructor when it's not being used? 2 the function binding is a very good lead and I went back and refactored my code changing over 17 handlers after reading the docs and an article where it stated that binding the functions in the render creates duplicate functions where as binding the functions in the constructor creates just one binding instance.

again, thanks for the help and your patience. PS: i've also watched some of your videos.... pretty good.

dcu-sharepoint avatar Mar 13 '17 22:03 dcu-sharepoint

why do I need to have props argument in the constructor when it's not being used?

If you don't pass them down, React.Component base class can’t possibly assign this.props in constructor. Some people get surprised by this.props not being available there so I’d recommend passing it down for consistency. Our longer term hope is that if class properties become part of the language (it’s currently a proposal), you could assign state without declaring the constructor altogether:

class Counter extends React.Component {
  state = { counter: 0 };
  handleClick = (e) => {
    e.preventDefault();
    console.log('Button:handleClick');
    this.setState({counter: 10});
  }
  // ...
}

As you can see, I declared handleClick with this experimental syntax as well, and this auto-binds it to this so that you wouldn’t need to bind it manually in the constructor. This is more similar to how methods work in Java/C#/etc. This syntax proposal is enabled by default in Create React App, but please keep in mind it’s experimental.

gaearon avatar Mar 13 '17 22:03 gaearon

Nice, it keeps the code cleaner. React is making me understand ES6 a heck of a lot more by sheer brute force since I'm in a tight schedule to learn the library to move on with another platform being put out by Microsoft using these tool chains and where Microsoft has basically chosen react as the basic UI library vs. Angular.... because of its lightweight features.

Tks again,

dcu-sharepoint avatar Mar 13 '17 23:03 dcu-sharepoint

Thanks Dan,

I'll play with it and put it to use when I understand it, I'm always looking for shortcuts...

LuisCarlos Rodriguez


From: Dan Abramov [email protected] Sent: Monday, March 13, 2017 6:44 PM To: facebook/react Cc: L. Carlos Rodriguez; Author Subject: Re: [facebook/react] how to define, create, assign, use, engineer or otherwise use either a props property or a state to a class component button (#9156)

why do I need to have props argument in the constructor when it's not being used?

If you don't pass them down, React.Component base class can’t possibly assign this.props in constructor. Some people get surprised by this.props not being available there so I’d recommend passing it down for consistency. Our longer term hope is that if class propertieshttps://github.com/tc39/proposal-class-public-fields become part of the language (it’s currently a proposal), you could assign state without declaring the constructor altogether:

class Counter extends React.Component { state = { counter: 0 }; handleClick = (e) => { e.preventDefault(); console.log('Button:handleClick'); this.setState({counter: 10}); } // ... }

As you can see, I declared handleClick with this experimental syntax as well, and this auto-binds it to this so that you wouldn’t need to bind it manually in the constructor. This is more similar to how methods work in Java/C#/etc. This syntax proposal is enabled by default in Create React App, but please keep in mind it’s experimental.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/facebook/react/issues/9156#issuecomment-286266619, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AVrwrwzVuCvwza5C_uZ_2CKqj5vqtPI1ks5rlcbcgaJpZM4MaFV0.

dcu-sharepoint avatar Mar 17 '17 11:03 dcu-sharepoint