我注意到在终极版使用,有时行动return等dispatch。我还注意到dispatch当该操作函数中有一些异步操作时使用它,例如从/向数据库读取/发布,但我不明白为什么。谁能解释一下关键的区别。dispatch例如,为什么我们不一直使用,例如 React 中的上下文 API?
编辑:我将添加一个示例,该示例不涉及后端,因此没有async await循环:
//with return
export const addLog = (log) => {
return{
type: ADD_LOG,
payload: log
}
}
//with dispatch
export const addLog = (log) => disptach => {
dispatch{
type: ADD_LOG,
payload: log
}
}
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?
我在 svelte 中有一个组件,我需要在其中定义一个名为 的值oldVal,该值不绑定到任何html元素,并将其初始化为值 X。下次组件渲染时,我想将其值更新为 X1,并且该值应该持续到下一次渲染,依此类推。在反应中我们使用useRef这个,但是如何在不编写存储的情况下以svelte实现这个?
编辑:添加一些代码
所以,基本上,我在组件内进行转换,其中我有一个自定义的 css 转换函数
<script>
export let tick;
export let tranX;
import {select} from 'd3-selection';
import {interpolate} from 'd3-interpolate';
// This value is initially 0 but then updated on each render
let oldVal =0;
//Custom CSS transition function
function animateX(node, {duration}){
const xTranslate = +select(node)
.attr('transform')
.split('(')[1]
.split(',')[0];//Here I extract the translate value in the x direction
const interVal =
oldVal > xTranslate
? interpolate(xTranslate , oldVal)
: …Run Code Online (Sandbox Code Playgroud)