Skip to content

Commit

Permalink
Implement two-way-binding
Browse files Browse the repository at this point in the history
- Implement a new way of get and handle some contents in different components
  • Loading branch information
fernandoferreiratbe committed Aug 12, 2019
1 parent 850ca9a commit 61b7d5c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/app/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Home extends React.Component {
this.state = {
age: props.initialAge,
status: 0,
homeLink: "Changed Link"
homeLink: props.initialLinkName
}

// Just to observe Virtual DOM updating behavior.
Expand All @@ -31,6 +31,12 @@ export class Home extends React.Component {
this.props.changeLink(this.state.homeLink);
}

onHandleChange(event) {
this.setState({
homeLink: event.target.value
});
}

render() {
return(
<div>
Expand All @@ -43,6 +49,7 @@ export class Home extends React.Component {
<hr />
<button onClick={this.props.greet} className="btn btn-primary">Greet</button>
<hr />
<input type="text" value={this.state.homeLink} onChange={(event) => this.onHandleChange(event)}/>
<button onClick={this.onChangeLink.bind(this)} className="btn btn-primary">Change Header Link</button>
</div>
);
Expand All @@ -52,5 +59,6 @@ export class Home extends React.Component {
Home.propTypes = {
name: PropTypes.string,
initialAge: PropTypes.number,
greet: PropTypes.func
greet: PropTypes.func,
initialLinkName: PropTypes.string
}
4 changes: 3 additions & 1 deletion src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class App extends React.Component {
name={"Fernando"}
initialAge={32}
greet={this.onGreet}
changeLink={this.onChangeLinkName.bind(this)} />
changeLink={this.onChangeLinkName.bind(this)}
initialLinkName={this.state.homeLink}
/>
</div>
</div>

Expand Down

0 comments on commit 61b7d5c

Please sign in to comment.