freeCodeCamp/curriculum/challenges/arabic/03-front-end-libraries/react/use-state-to-toggle-an-elem...

3.1 KiB

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036176 Use State to Toggle an Element 6 false

Description

يمكنك استخدام state في تطبيقات React بطرق أكثر تعقيدًا من تلك التي شاهدتها حتى الآن. أحد الأمثلة على ذلك هو مراقبة حالة القيمة ، ثم عرض واجهة المستخدم بشكل مشروط بناءً على هذه القيمة. هناك عدة طرق مختلفة لإنجاز هذا ، ويظهر محرر الشفرة طريقة واحدة.

Instructions

undefined

Tests

tests:
  - text: ''
    testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MyComponent)).find("div").find("button").length, 1, "<code>MyComponent</code> should return a <code>div</code> element which contains a <code>button</code>.");'
  - text: يجب تهيئة حالة <code>MyComponent</code> مع تعيين خاصية <code>visibility</code> إلى <code>false</code> .
    testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MyComponent)).state("visibility"), false, "The state of <code>MyComponent</code> should initialize with a <code>visibility</code> property set to <code>false</code>.");'
  - text: ''
    testString: 'async () => { const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250)); const mockedComponent = Enzyme.mount(React.createElement(MyComponent)); const first = () => { mockedComponent.setState({ visibility: false }); return waitForIt(() => mockedComponent.state("visibility")); }; const second = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent.state("visibility")); }; const third = () => { mockedComponent.find("button").simulate("click"); return waitForIt(() => mockedComponent.state("visibility")); }; const firstValue = await first(); const secondValue = await second(); const thirdValue = await third(); assert(!firstValue && secondValue && !thirdValue, "Clicking the button element should toggle the <code>visibility</code> property in state between <code>true</code> and <code>false</code>."); }; '

Challenge Seed

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visibility: false
    };
    // change code below this line

    // change code above this line
  }
  // change code below this line

  // change code above this line
  render() {
    if (this.state.visibility) {
      return (
        <div>
          <button onClick={this.toggleVisibility}>Click Me</button>
          <h1>Now you see me!</h1>
        </div>
      );
    } else {
      return (
        <div>
          <button onClick={this.toggleVisibility}>Click Me</button>
        </div>
      );
    }
  }
};

After Test

console.info('after the test');

Solution

// solution required