Skip to content

Commit f25a9fb

Browse files
committed
Added logic to wire-up the 'Change text...' button to prompt the user for the text when clicked, and to update the <TextWidget> 'state' with the text entered, to cause it to be re-rendered by React with that new text.
1 parent 89ecb68 commit f25a9fb

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

webpack_in/entry.jsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ const objStyleContent = {
2525
class ButtonWidget extends React.Component {
2626
render() {
2727
return (
28-
<button style={{ margin: '1em'}}>{ this.props.caption }</button>
28+
<button style={{ margin: '1em'}}
29+
onClick={ this.props.onClick }>{ this.props.caption }</button>
2930
);
3031
}
3132
}
3233

3334
ButtonWidget.propTypes = {
34-
caption: PropTypes.string.isRequired
35+
caption: PropTypes.string.isRequired,
36+
onClick: PropTypes.func
3537
};
3638

3739
class HelloWidget extends React.Component {
@@ -57,7 +59,16 @@ class TextWidget extends React.Component {
5759
return (
5860
<div style={ objStyleCommon }>
5961
<div style={ objStyleContent }>{ this.state.text }</div>
60-
<ButtonWidget caption="Change text..." />
62+
<ButtonWidget caption="Change text..."
63+
onClick={() => {
64+
const strTextNew = prompt("Please enter text to display:",
65+
this.state.text);
66+
if (strTextNew === null) {
67+
return;
68+
}
69+
70+
this.setState({...this.state, text: strTextNew});
71+
}}/>
6172
</div>
6273
);
6374
}

0 commit comments

Comments
 (0)