1

I have a small but interesting problem which I can't wrap my head around. I have a custom DatePicker component which uses TValue as Value (generic, you can pass DateTime or DateTime? as TValue). And inside a calendar I have button 'x' which clears the Value to default of TValue

    [Parameter]
    public EventCallback<TValue> ValueChanged { get; set; }
    
    protected async void ClearInputData()
    {
        await ValueChanged.InvokeAsync(default(TValue));
        Value = default(TValue);
        selectedHour = 0;
        selectedMinute = 0;
        selectedSecond = 0;
        OnInput?.Invoke();
    }

The problem is that default(TValue) if TValue == DateTime is null, and you cannot invoke null; What could be the smartest way ot getting around this?

4
  • check if the value is null if so dont invoke ! Commented Jun 30, 2021 at 13:31
  • but the place where I invoke that value I need to know that the value is unselected. For example, I have typed 2020-06-06, and when I press x I need to inform my other component, that the value now is null Commented Jun 30, 2021 at 13:32
  • 1
    Start by making it an async Task method and see if the problem persists. If so, post exact error messages or other results. Commented Jun 30, 2021 at 14:46
  • Ah, I found the problem. It was a deeper problem. I'm also using custom grid, and inside inserting template I was using ValueChanged=((args) => data.Date = args.Value). And args was null and Value would crash... Sorry for such a dumb error, that's totally my bad... Lesson learn, debug your code deeper Commented Jun 30, 2021 at 20:03

1 Answer 1

2

I tried to reproduce your problem.

My DateComponent:

    <h3>DateComponent</h3>
    
    @typeparam TValue
    <p>Value: @Value</p>
    <button @onclick="() => ClearInputData()">X</button>
    
    @code {
        [Parameter]
        public EventCallback<TValue> ValueChanged { get; set; }
    
        [Parameter]
        public TValue Value { get; set; }
    
        protected async Task ClearInputData()
        {
            await ValueChanged.InvokeAsync(default(TValue));
            Value = default(TValue);
        }
    }

My consumer:

    <DateComponent TValue="DateTime?" @bind-Value="@Value1"></DateComponent>
    <p>@(Value1.HasValue ? Value1 : "(null)")</p>
    
    <DateComponent TValue="DateTime" @bind-Value="@Value2"></DateComponent>
    <p>@Value2</p>
    
    @code {
        DateTime? Value1 { get; set; } = DateTime.Now;
        DateTime Value2 { get; set; } = DateTime.Now;
    }

In my example, everything works as expected. Did I miss something?

Sign up to request clarification or add additional context in comments.

5 Comments

Can you try again with async void ClearInputData() ?
Mhm.... Okey, I will try to implement, maybe my problem might be on the other end
Worked like a charm. A problem was because of void, Task fixed it. EDIT: I found, my other component was the problem. I think that datepicker component was working as it should, but my custom grid was getting an error here
@HenkHolterman: It doesn't matter whether void or task is returned.
Leave the Task in anyway. Just for coy/pasters.

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.