freeCodeCamp/curriculum/challenges/arabic/03-front-end-libraries/react/manage-updates-with-lifecyc...

6.4 KiB

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d403617f Manage Updates with Lifecycle Methods 6 false إدارة التحديثات باستخدام أساليب دورة الحياة

Description

هناك طريقة أخرى لدورة الحياة هي componentWillReceiveProps() والتي يتم استدعاؤها كلما تلقى المكون الدعائم الجديدة. يتلقى هذا الأسلوب الدعائم الجديدة كوسيطة ، والتي عادة ما تتم كتابتها على شكل nextProps . يمكنك استخدام هذه الوسيطة ومقارنتها مع هذا this.props وتنفيذ الإجراءات قبل تحديث المكون. على سبيل المثال ، يمكنك استدعاء setState() محليًا قبل معالجة التحديث. طريقة أخرى هي componentDidUpdate() ، وتسمى على الفور بعد إعادة عرض المكون. لاحظ أن التقديم والتثبيت يعتبران أشياء مختلفة في دورة حياة المكونات. عندما يتم تحميل الصفحة أولاً ، يتم تحميل جميع المكونات وهذا هو المكان الذي يتم فيه استدعاء أساليب مثل componentWillMount() و componentDidMount() . بعد ذلك ، مع تغير الدولة ، تقوم المكونات بإعادة تقديم نفسها. التحدي التالي يغطي هذا بمزيد من التفصيل.

Instructions

يتلقى مربع Dialog المكون التابع الدعائم message من أصلها ، مكون " Controller . اكتب أسلوب componentWillReceiveProps() في مكون " Dialog وقم this.props و nextProps إلى وحدة التحكم. ستحتاج إلى تمرير nextProps كحجة لهذه الطريقة ، وعلى الرغم من أنه من الممكن تسمية أي شيء ، nextProps ذلك هنا. بعد ذلك ، قم بإضافة componentDidUpdate() في مكون Dialog ، وقم بتسجيل عبارة تقول أن المكون قد تم تحديثه. تعمل هذه الطريقة على غرار componentWillUpdate() ، الذي يتم توفيره لك. الآن انقر فوق الزر لتغيير الرسالة ومشاهدة وحدة تحكم المستعرض الخاص بك. يعرض ترتيب عبارات وحدة التحكم الترتيب الذي تسمى الطرق. ملاحظة: ستحتاج إلى كتابة أساليب دورة الحياة كوظائف عادية وليس كدالة سهم لاجتياز الاختبارات (لا توجد أيضًا ميزة لكتابة أساليب دورة الحياة كدالة سهم).

Tests

tests:
  - text: يجب أن يقوم مكون &quot; <code>Controller</code> بعرض مكون &quot; <code>Dialog</code> &quot; كطفل.
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Controller)); return mockedComponent.find("Controller").length === 1 && mockedComponent.find("Dialog").length === 1; })(), "The <code>Controller</code> component should render the <code>Dialog</code> component as a child.");'
  - text: يجب أن يتم تسجيل أسلوب <code>componentWillReceiveProps</code> في مكون <code>Dialog</code> <code>this.props</code> إلى وحدة التحكم.
    testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentWillReceiveProps.toString().replace(/ /g,""); return lifecycleChild.includes("console.log") && lifecycleChild.includes("this.props") })(), "The <code>componentWillReceiveProps</code> method in the <code>Dialog</code> component should log <code>this.props</code> to the console.");'
  - text: يجب أن يسجل الأسلوب <code>componentWillReceiveProps</code> في مكون <code>Dialog</code> <code>nextProps</code> إلى وحدة التحكم.
    testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentWillReceiveProps.toString().replace(/ /g,""); const nextPropsAsParameterTest = /componentWillReceiveProps(| *?= *?)(\(|)nextProps(\)|)( *?=> *?{| *?{|{)/; const nextPropsInConsoleLogTest = /console\.log\(.*?nextProps\b.*?\)/; return ( lifecycleChild.includes("console.log") && nextPropsInConsoleLogTest.test(lifecycleChild) && nextPropsAsParameterTest.test(lifecycleChild) ); })(), "The <code>componentWillReceiveProps</code> method in the <code>Dialog</code> component should log <code>nextProps</code> to the console.");'
  - text: يجب استدعاء مكون <code>Dialog</code> أسلوب <code>componentDidUpdate</code> وتسجيل رسالة إلى وحدة التحكم.
    testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentDidUpdate.toString().replace(/ /g,""); return lifecycleChild.length !== "undefined" && lifecycleChild.includes("console.log"); })(), "The <code>Dialog</code> component should call the <code>componentDidUpdate</code> method and log a message to the console.");'

Challenge Seed

class Dialog extends React.Component {
  constructor(props) {
    super(props);
  }
  componentWillUpdate() {
    console.log('Component is about to update...');
  }
  // change code below this line

  // change code above this line
  render() {
    return <h1>{this.props.message}</h1>
  }
};

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 (
      <div>
        <button onClick={this.changeMessage}>Update</button>
        <Dialog message={this.state.message}/>
      </div>
    );
  }
};

After Test

console.info('after the test');

Solution

// solution required