1

I'm learning how to use the onPush detection strategy. My view updates fine, but the variables on my child.component.ts values don't, but they do on the child.component.html

CHILD COMPONENT

@Component({
  ....
  changeDetection: ChangeDetectionStrategy.OnPush
})

export class BdayFormComponent implements OnInit {

  @Input() question: Question;
  @Output() nextQuestion = new EventEmitter<Question>();


  nextQuestions(): void {
    ...
    this.nextQuestion.emit(......)
    console.log(this.question);
  }
}

As you can see nextQuestion, tells the parent component to dispatch an action to get new question. I can see the next question on my HTML (let's say question #2), but when I do the console.log(this.questions); it prints the question #1.

PARENT COMPONENT

export class BdayShellComponent implements OnInit {
  question$: Observable<Question>;

  constructor(private store: Store<fromProduct.State>) { }

  ngOnInit(): void {
    this.question$ = this.store.pipe(select(fromProduct.getCurrentQuestion));
  }

  setCurrentQuestion(question: Question): void {
    this.store.dispatch(new bdayActions.SetCurrentQuestion(question));
  }
}

PARENT COMPONENT HTML

<app-bday-form [question]="question$ | async"
        (nextQuestion)="setCurrentQuestion($event)"></app-bday-form>

1 Answer 1

1

At the time when you put the log statement, the component didn't receive the new input.

Try the following, and you'll see the value being logged

  ngOnChanges(changes: SimpleChanges) {
    console.log(changes.question)
  }
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.