reactjs - setting State without using setState in React.js -


i found tutorial online regarding on react.js saw way tutor did change state without setstate() function. here asking whether practice.

the constructor:

constructor(props) {     super(props);     this.state = {       uid: uuid.v1(),       name: '',       answers: {           answer1: '',           answer2: '',           answer3: ''       },       issubmitted: false     };     this.onsubmit = this.onsubmit.bind(this);     this.answerselected = this.answerselected.bind(this);     this.questionsubmitted = this.questionsubmitted.bind(this); } 

the way update state is:-

answerselected(event) {     let answers = this.state.answers;     if (event.target.name === 'answer1') {         answers.answer1 = event.target.value;     } else if (event.target.name === 'answer2') {         answers.answer2 = event.target.value;     } else if (event.target.name === 'answer3') {         answers.answer3 = event.target.value;     } } 

from know, react should set state using setstate({answers: {answer1: event.target.value}}). right? better approach?

setting/changing state inside react component should using setstate, react recommends. place state can directly modified constructor. read more here.

also worth reading this


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

nginx - phpPgAdmin - log in works but I have to login again after clicking on any links -