--- id: 5a24c314108439a4d4036176 title: Use State to Toggle an Element challengeType: 6 isRequired: false videoUrl: '' localeTitle: '' --- ## Description
يمكنك استخدام state في تطبيقات React بطرق أكثر تعقيدًا من تلك التي شاهدتها حتى الآن. أحد الأمثلة على ذلك هو مراقبة حالة القيمة ، ثم عرض واجهة المستخدم بشكل مشروط بناءً على هذه القيمة. هناك عدة طرق مختلفة لإنجاز هذا ، ويظهر محرر الشفرة طريقة واحدة.
## Instructions undefined ## Tests
```yml tests: - text: '' testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MyComponent)).find("div").find("button").length, 1, "MyComponent should return a div element which contains a button.");' - text: يجب تهيئة حالة MyComponent مع تعيين خاصية visibility إلى false . testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MyComponent)).state("visibility"), false, "The state of MyComponent should initialize with a visibility property set to false.");' - 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 visibility property in state between true and false."); }; ' ```
## Challenge Seed
```jsx 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 (

Now you see me!

); } else { return (
); } } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```