0

I'm trying to assign a object to initialState variable, the selectedActivity type is Activity | undefined and after Nullish Coalescing operator (??) the emptyActivity is of type Activity. but when this line execute, it throws an error that says: Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. Code:

interface Props {
    selectedActivity: Activity | undefined
    closeForm: () => void
}
export default function ActivityForm({ selectedActivity, closeForm }: Props) {
    const emptyActivity: Activity = {
        id: '',
        title: '',
        date: '',
        description: '',
        category: '',
        city: '',
        venue: '',
    }
const initialState = selectedActivity ?? emptyActivity;

and this is Activity interface:

export interface Activity {
    id: string
    title: string
    date: string
    description: string
    category: string
    city: string
    venue: string
}

I'm coding a react project and I want to solve the assignment error.

1 Answer 1

1

As the error said that your loader can't handle this syntax, maybe it is old, and you need to update it, but as a temporary solution you can convert this syntax to ternary operator syntax and it should work.

const initialState = selectedActivity ? selectedActivity : emptyActivity;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.