Irmantas Želionis
Irmantas Želionis

Reputation: 2324

Use value from PropsFromState in state constructor

I have this props interface:

interface OwnProps {
  onHide: () => void;
  show: boolean;
  onSubmit: (props: SetCompanyUploaderFormSchema) => void;
  client: Client;
}

And this PropsFromState interface:

interface PropsFromState {
  userList: DropDownListItem<string>[];
  userListIds: Array<number>;
}

Which I initialize like this:

const mapStateToProps = (state: State, props: OwnProps): PropsFromState => ({
  userList: getCompanyTeamListSelector(state),
  userListIds: state.ddls.companyUsers.map(element => Number(element.id))
});

I am trying to set state value selectedUsersIds to PropsFromState value userListIds.

I tried to do this in the constructor:

@connect(mapStateToProps)
export default class EmailModal extends React.Component<OwnProps & Partial<PropsFromDispatch> & Partial<PropsFromState>, OwnState> {
  constructor(propsFromState: PropsFromState, props: OwnProps ) {
    super(props);
    this.state = {
      selectedUserIds: propsFromState.userListIds
    }
  }

I tried this.props.userListIds as well.

In addition to that I tried to set it by using getDerivedStateFromProps and ComponentWillMount. Nothing worked so far and this.state.selecteduserIds is still comes up as empty array.

However, when I print this.props.userListIds at the beginning of the render() it shows up correctly.

Where is the problem?

Upvotes: 0

Views: 152

Answers (1)

Frank
Frank

Reputation: 179

constructor(props:propsFromState) {}

And then call super(props);

Upvotes: 1

Related Questions