diff --git a/curriculum/challenges/_meta/react/meta.json b/curriculum/challenges/_meta/react/meta.json index fce191d6e2..8d498ff3c3 100644 --- a/curriculum/challenges/_meta/react/meta.json +++ b/curriculum/challenges/_meta/react/meta.json @@ -151,10 +151,6 @@ "5a24c314108439a4d403617e", "Add Event Listeners" ], - [ - "5a24c314108439a4d403617f", - "Manage Updates with Lifecycle Methods" - ], [ "5a24c314108439a4d4036180", "Optimize Re-Renders with shouldComponentUpdate" @@ -210,4 +206,4 @@ ], "helpRoom": "Help", "fileName": "03-front-end-libraries/react.json" -} \ No newline at end of file +} diff --git a/curriculum/challenges/english/03-front-end-libraries/react/manage-updates-with-lifecycle-methods.english.md b/curriculum/challenges/english/03-front-end-libraries/react/manage-updates-with-lifecycle-methods.english.md deleted file mode 100644 index efd2252f47..0000000000 --- a/curriculum/challenges/english/03-front-end-libraries/react/manage-updates-with-lifecycle-methods.english.md +++ /dev/null @@ -1,149 +0,0 @@ ---- -id: 5a24c314108439a4d403617f -title: Manage Updates with Lifecycle Methods -challengeType: 6 -isRequired: false -forumTopicId: 301397 ---- - -## Description -
- Warning: componentWillReceiveProps() and componentWillUpdate() are deprecated since their usage can lead to bugs and inconsistencies. They were replaced to, respectively, UNSAFE_componentWillReceiveProps() and UNSAFE_componentWillUpdate() and they should be avoided in new code. -Another lifecycle method is UNSAFE_componentWillReceiveProps() which is called whenever a component is receiving new props. This method receives the new props as an argument, which is usually written as nextProps. You can use this argument and compare with this.props and perform actions before the component updates. For example, you may call setState() locally before the update is processed. -Another method is componentDidUpdate(), and is called immediately after a component re-renders. Note that rendering and mounting are considered different things in the component lifecycle. When a page first loads, all components are mounted and this is where methods like componentWillMount() and componentDidMount() are called. After this, as state changes, components re-render themselves. The next challenge covers this in more detail. -
- -## Instructions -
-The child component Dialog receives message props from its parent, the Controller component. Write the UNSAFE_componentWillReceiveProps() method in the Dialog component and have it log this.props and nextProps to the console. You'll need to pass nextProps as an argument to this method and although it's possible to name it anything, name it nextProps here. -Next, add componentDidUpdate() in the Dialog component, and log a statement that says the component has updated. This method works similar to UNSAFE_componentWillUpdate(), which is provided for you. Now click the button to change the message and watch your browser console. The order of the console statements show the order the methods are called. -Note: You'll need to write the lifecycle methods as normal functions and not as arrow functions to pass the tests (there is also no advantage to writing lifecycle methods as arrow functions). -
- -## Tests -
- -```yml -tests: - - text: The Controller component should render the Dialog component as a child. - testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Controller)); return mockedComponent.find('Controller').length === 1 && mockedComponent.find('Dialog').length === 1; })(), 'The Controller component should render the Dialog component as a child.'); - - text: The UNSAFE_componentWillReceiveProps method in the Dialog component should log this.props to the console. - testString: assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.UNSAFE_componentWillReceiveProps.toString().replace(/ /g,''); return lifecycleChild.includes('console.log') && lifecycleChild.includes('this.props') })(), 'The UNSAFE_componentWillReceiveProps method in the Dialog component should log this.props to the console.'); - - text: The UNSAFE_componentWillReceiveProps method in the Dialog component should log nextProps to the console. - testString: assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.UNSAFE_componentWillReceiveProps.toString().replace(/ /g,''); const nextPropsAsParameterTest = /UNSAFE_componentWillReceiveProps(| *?= *?)(\(|)nextProps(\)|)( *?=> *?{| *?{|{)/; const nextPropsInConsoleLogTest = /console\.log\(.*?nextProps\b.*?\)/; return ( lifecycleChild.includes('console.log') && nextPropsInConsoleLogTest.test(lifecycleChild) && nextPropsAsParameterTest.test(lifecycleChild) ); })(), 'The UNSAFE_componentWillReceiveProps method in the Dialog component should log nextProps to the console.'); - - text: The Dialog component should call the componentDidUpdate method and log a message to the console. - testString: assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentDidUpdate.toString().replace(/ /g,''); return lifecycleChild.length !== 'undefined' && lifecycleChild.includes('console.log'); })()); - -``` - -
- -## Challenge Seed -
- -
- -```jsx -class Dialog extends React.Component { - constructor(props) { - super(props); - } - UNSAFE_componentWillUpdate() { - console.log('Component is about to update...'); - } - // change code below this line - - // change code above this line - render() { - return

{this.props.message}

- } -}; - -class Controller extends React.Component { - constructor(props) { - super(props); - this.state = { - message: 'First Message' - }; - this.changeMessage = this.changeMessage.bind(this); - } - changeMessage() { - this.setState({ - message: 'Second Message' - }); - } - render() { - return ( -
- - -
- ); - } -}; -``` - -
- - -### After Test -
- -```js -ReactDOM.render(, document.getElementById('root')) -``` - -
- -
- -## Solution -
- - -```js -class Dialog extends React.Component { - constructor(props) { - super(props); - } - UNSAFE_componentWillUpdate() { - console.log('Component is about to update...'); - } - // change code below this line - UNSAFE_componentWillReceiveProps(nextProps) { - console.log(this.props, nextProps); - } - componentDidUpdate() { - console.log('Component re-rendered'); - } - // change code above this line - render() { - return

{this.props.message}

- } -}; - -class Controller extends React.Component { - constructor(props) { - super(props); - this.state = { - message: 'First Message' - }; - this.changeMessage = this.changeMessage.bind(this); - } - changeMessage() { - this.setState({ - message: 'Second Message' - }); - } - render() { - return ( -
- - -
- ); - } -}; -``` - -
diff --git a/curriculum/challenges/english/03-front-end-libraries/react/optimize-re-renders-with-shouldcomponentupdate.english.md b/curriculum/challenges/english/03-front-end-libraries/react/optimize-re-renders-with-shouldcomponentupdate.english.md index 04ef806ffc..8f15053042 100644 --- a/curriculum/challenges/english/03-front-end-libraries/react/optimize-re-renders-with-shouldcomponentupdate.english.md +++ b/curriculum/challenges/english/03-front-end-libraries/react/optimize-re-renders-with-shouldcomponentupdate.english.md @@ -14,7 +14,7 @@ This method is a useful way to optimize performance. For example, the default be ## Instructions
-The shouldComponentUpdate() method is added in a component called OnlyEvens. Currently, this method returns true so OnlyEvens re-renders every time it receives new props. Modify the method so OnlyEvens updates only if the value of its new props is even. Click the Add button and watch the order of events in your browser's console as the other lifecycle hooks are triggered. +The shouldComponentUpdate() method is added in a component called OnlyEvens. Currently, this method returns true so OnlyEvens re-renders every time it receives new props. Modify the method so OnlyEvens updates only if the value of its new props is even. Click the Add button and watch the order of events in your browser's console as the lifecycle hooks are triggered.
## Tests @@ -51,9 +51,6 @@ class OnlyEvens extends React.Component { return true; // change code above this line } - componentWillReceiveProps(nextProps) { - console.log('Receiving new props...'); - } componentDidUpdate() { console.log('Component re-rendered.'); } @@ -115,9 +112,6 @@ class OnlyEvens extends React.Component { return nextProps.value % 2 === 0; // change code above this line } - componentWillReceiveProps(nextProps) { - console.log('Receiving new props...'); - } componentDidUpdate() { console.log('Component re-rendered.'); }