freeCodeCamp/curriculum/challenges/arabic/03-front-end-libraries/react/render-with-an-if-else-cond...

4.0 KiB

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036184 Render with an If-Else Condition 6 false

Description

undefined

Instructions

يحتوي MyComponent على boolean في حالته التي تقوم بتتبع ما إذا كنت تريد عرض بعض العناصر في واجهة المستخدم أم لا. يقوم button تبديل حالة هذه القيمة. حاليًا ، يتم عرض واجهة المستخدم نفسها في كل مرة. أعد كتابة طريقة render() مع عبارة if/else بحيث إذا كان display true ، فأعدت الترميز الحالي. وإلا ، فأعد الترميز بدون عنصر h1 . ملاحظة: يجب عليك كتابة if/else لتمرير الاختبارات. استخدام المشغل الثلاثي لن يمر هنا.

Tests

tests:
  - text: يجب أن يوجد <code>MyComponent</code> وعرض.
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); return mockedComponent.find("MyComponent").length === 1; })(), "<code>MyComponent</code> should exist and render.");'
  - text: ''
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); const state_1 = () => { mockedComponent.setState({display: true}); return waitForIt(() => mockedComponent )}; const updated = await state_1(); assert(mockedComponent.find("div").length === 1 && mockedComponent.find("div").children().length === 2 && mockedComponent.find("button").length === 1 && mockedComponent.find("h1").length === 1, "When <code>display</code> is set to <code>true</code>, a <code>div</code>, <code>button</code>, and <code>h1</code> should render."); }; '
  - text: عند تعيين <code>display</code> على &quot; <code>false</code> ، يجب أن يتم <code>display</code> فقط <code>button</code> <code>div</code> <code>button</code> .
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); const state_1 = () => { mockedComponent.setState({display: false}); return waitForIt(() => mockedComponent )}; const updated = await state_1(); assert(mockedComponent.find("div").length === 1 && mockedComponent.find("div").children().length === 1 && mockedComponent.find("button").length === 1 && mockedComponent.find("h1").length === 0, "When <code>display</code> is set to <code>false</code>, only a <code>div</code> and <code>button</code> should render."); }; '
  - text: يجب أن تستخدم طريقة التجسيد عبارة <code>if/else</code> للتحقق من حالة <code>this.state.display</code> .
    testString: 'getUserInput => assert(getUserInput("index").includes("if") && getUserInput("index").includes("else"), "The render method should use an <code>if/else</code> statement to check the condition of <code>this.state.display</code>.");'

Challenge Seed

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: true
    }
    this.toggleDisplay = this.toggleDisplay.bind(this);
  }
  toggleDisplay() {
    this.setState({
      display: !this.state.display
    });
  }
  render() {
    // change code below this line

    return (
       <div>
         <button onClick={this.toggleDisplay}>Toggle Display</button>
         <h1>Displayed!</h1>
       </div>
    );
  }
};

After Test

console.info('after the test');

Solution

// solution required