I am trying to populate the options parameter of the Dropdown component from a state but nothing shows in Dropdown because the state is empty when the Dropdown get rendered.
this is how I am creating the state:
export interface IListState {
views: IView[];
}
and this is how I am intializing it in the constructor:
this.state = {
views: []
};
Then in the componentDidMount:
public componentDidMount(): void {
this.props.provider.getViews().then((views: IView[]) => {
this.setState({
views: views
})
});
}
And finnaly in the render I am using the Dropdown like this:
<Dropdown
label="Disabled example with defaultSelectedKey"
defaultSelectedKey=""
options={this.state.views}
disabled={false}
/>
The problem is when the web part get rendered in the console I can see that the state is empty and then it gets filled with data but the Dropdown was already rendered so it doesn't get the the data.
How can I make the dropdown read the state to get the data for the option property?
UPDATED CODE:
I updated the code and set the dropdown in a method and this how I am calling it from the render():
<div>
{ this._renderDropdown() }
</div>
and this is hoe the method looks like:
private _renderDropdown(): any {
const {views} = this.state;
console.log(views);
if(views.length > 0) {
return(
<Dropdown
label="Disabled example with defaultSelectedKey"
defaultSelectedKey=""
options={views}
disabled={false}
/>
)
}
}
But the result is exactly the same :-(
Best regards Americo