Question 3. Pass the event handler


#1


https://www.codecademy.com/en/courses/react-102/lessons/child-updates-parents-state/exercises/child-parent-pass-handler?action=resume


Not sure why I cannot get the following code working in the Parent.js

  1. Pass changeName down to Child!

In Parent.js, inside of Parent's render function, add a second attribute to . Give this attribute a name of onChange, and a value of the changeName function.


var React = require('react');
var ReactDOM = require('react-dom');
var Child = require('./Child');

var Parent = React.createClass({
  getInitialState: function () {
    return { name: 'Frarthur' };
  },

  changeName: function (newName) {
  this.setState({
    name: newName
  	});
  },

  render: function () {
    return (
    	<Child 
    		name={this.state.name},
        onChange={this.changeName(this.state.name)}/>
    );
  }
});

ReactDOM.render(
	<Parent />, 
	document.getElementById('app')
);


#2

No comma needed between attributes (I don't think).
Also: onChange={this.changeName} since you're just passing the function there.


#3

Thanks Melissa It works now


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.