Home >Web Front-end >JS Tutorial >How to use take in redux-saga

How to use take in redux-saga

小云云
小云云Original
2018-03-01 08:58:411607browse

This article mainly introduces you to the detailed explanation of the use of take in redux-saga. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Bring me an API usage method that I have studied for a long time.

The take API usage method in the effect in redux-saga is mostly used for call, put, and select, but the take method is more common. I really don’t have a chance to use it, and I don’t know where to use it. Anyway, since it is written by redux-saga, there must be its usage. No matter 37 21, learn how to use it first.

Let’s take a look at the introduction first:

take

take’s performance is the same as takeEvery, it monitors an action, but unlike takeEvery, it is not triggered every time the action is triggered. All correspond, but the corresponding action will only occur when the execution sequence reaches the take statement.

When using the take statement in the genetator to wait for an action, the generator is blocked, waiting for the action to be distributed, and then continues execution.

takeEvery just listens to each action and then executes the processing function. takeEvery has no control over when and how to respond.

But take is different. We can decide in the generator function when to respond to an action and what to do after an action is triggered.

The biggest difference: take will only respond to the corresponding action when the execution flow is reached, while takeEvery will respond to the action once registered.

Code above:


effects: {
 * takeDemo1({payload}, {put, call, take}) {

 },
 * takeInputChange({payload}, {put, call, take,takeEvery,takeLatest}) {
  // yield call(delay,1000);
  console.log(takeEvery);
  // for (let i = 0; i < 3; i++) {
   const action = yield take(&#39;takeBlur&#39;});
   console.log(action, &#39;action&#39;);
   console.log(payload.value);
  // }

 },
 * takeBlur() {
  console.log(323)
 },
}


changeHandle(e){
 this.props.dispatch({type:&#39;takeInputChange&#39;,payload:{value:e.target.value}})
}
blur(){
 this.props.dispatch({type:&#39;takeBlur&#39;})
}
render() {

 return (
  <p style={{position: &#39;relative&#39;}}>
   <Input onChange={this.changeHandle.bind(this)} onBlur={this.blur.bind(this)}/> 
  </p>
  )
}

There is an input on the page, bound to two methods, the first One is the onchange method, and the other is the onBlur method.

When the input value changes, this function is called through this.props.dispatch({type:'takeInputChange'}), but because the take method is encountered , the execution cannot continue (paused). If the take here is replaced by takeEvery, it will be very different. The function will continue to execute, that is, the following two consoles will execute,

And the method of execution of takeEvery Then put it in its callback, look at the following code


yield takeEvery(&#39;takeBlur&#39;,()=>{console.log(payload.value)});

It needs to be emphasized that this function will be triggered every time the input changes, so every time it changes, You will see that the console will print the value in the console once.

Next, if the input loses focus, the onBlur method will be executed, and this.props.dispatch({type:'takeBlur'} will be called. );

Because the take in takeInputChange has monitored the takeBlur action, it will continue to execute the content that needs to be executed.

This take has been studied for a long time anyway, I don’t know When can this thing come in handy?

The above is the detailed content of How to use take in redux-saga. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn