Hello everyone,
I am very new to React and I need to create a app that uses a stateless component to show a dropdown. The code for the dropdown is in this Microsoft link: developer.microsoft.com/en-us/fabric My question is how can I make this component stateless? Is it possible? I saw other samples where people show how to write stateless component and in my test they works fine but they are no so complex like the one in the MS page. In my test I just render a simple text. The difference between my test code and the one in the MS page is that in the MS the component is extending a BaseComponent and I don't know how to do that in the stateless test I have.
Right now my test code is like this:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import styles from './Footer.module.scss';
const footer = () => {
return (
<div className={styles.footer}>
<div className={styles.main}>
<div className="ms-Grid">
<div className="ms-Grid-row">
<div className="ms-Grid-col ms-sm6 ms-md8 ms-lg10">
<p>other text</p>
</div>
<div className="ms-Grid-col ms-sm6 ms-md4 ms-lg2">
<p>DropDown</p>
</div>
</div>
</div>
</div>
</div>
)
};
export default footer;
Best reagards Americo
Hi again, I finally I managed to make the dropdown react component work in my app. Now I have another dilema. I need to find a way to insert data into the dropdown. This is how my component looks like now:
import * as React from 'react'; import { Dropdown, DropdownMenuItemType } from "office-ui-fabric-react/lib/Dropdown"; import { BaseComponent } from "office-ui-fabric-react/lib/Utilities"; import styles from './Footer.module.scss'; const footer = (props) => { return ( <div className={styles.footer}> <div className={styles.main}> <div className="ms-Grid"> <div className="ms-Grid-row"> <div className="ms-Grid-col ms-sm6 ms-md8 ms-lg10"> <p>Other text</p> </div> <div className="ms-Grid-col ms-sm6 ms-md4 ms-lg2"> <Dropdown placeHolder="select a option" options={[ { key: 'Header', text: 'Actions', itemType: DropdownMenuItemType.Header }, { key: 'A', text: 'Option a', title: 'I am option a.' }, { key: 'B', text: 'Option b' }, { key: 'C', text: 'Option c' }, { key: 'D', text: 'Option d' }, { key: 'E', text: 'Option e' } ]} /> </div> </div> </div> </div> </div> ) } export default footer;I found a sample at MS home page that show how to create mockupDataProvider:
import { ISPList } from './HelloWorldWebPart'; export default class MockHttpClient { private static _items: ISPList[] = [{ Title: 'Mock List', Id: '1' }, { Title: 'Mock List 2', Id: '2' }, { Title: 'Mock List 3', Id: '3' }]; public static get(): Promise<ISPList[]> { return new Promise<ISPList[]>((resolve) => { resolve(MockHttpClient._items); }); } }And how to implement it:
private _renderList(items: ISPList[]): void { let html: string = ''; items.forEach((item: ISPList) => { html += ` <ul class="${styles.list}"> <li class="${styles.listItem}"> <span class="ms-font-l">${item.Title}</span> </li> </ul>`; }); const listContainer: Element = this.domElement.querySelector('#spListContainer'); listContainer.innerHTML = html; }As you see in the code sample, it is using a forEach to loop through the objects in the ISPList array.
My issue is how I replace this (from the dropdown component):
options={[ { key: 'Header', text: 'Actions', itemType: DropdownMenuItemType.Header }, { key: 'A', text: 'Option a', title: 'I am option a.' }, { key: 'B', text: 'Option b' }, { key: 'C', text: 'Option c' }, { key: 'D', text: 'Option d' }, { key: 'E', text: 'Option e' } ]}With a loop like in the MS sample?
Here is the link to the MS sample for more details: Link