提交 9fc43c68 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat(composition api): customRef

上级 3199ca6a
const PAGE_PATH = '/pages/composition-api/reactivity/custom-ref/custom-ref'
describe('customRef', () => {
if (process.env.uniTestPlatformInfo.startsWith('android')) {
let page = null
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor('view')
})
it('basic', async () => {
const stateCount = await page.$('#state-count')
expect(await stateCount.text()).toBe('state.count: 0')
const incrementBtn = await page.$('#increment-btn')
await incrementBtn.tap()
expect(await stateCount.text()).toBe('state.count: 0')
const triggerRefBtn = await page.$('#trigger-ref-btn')
await triggerRefBtn.tap()
expect(await stateCount.text()).toBe('state.count: 1')
})
} else {
it('other platform', () => {
expect(1).toBe(1)
})
}
})
\ No newline at end of file
<template><view class="page">customRef</view></template>
\ No newline at end of file
<template>
<view class="page">
<text id="state-count">state.count: {{state['count']}}</text>
<button class="uni-common-mt" id="increment-btn" @click="increment">increment state.count</button>
<button class="uni-common-mt" id="trigger-ref-btn" @click="triggerRefState">triggerRef state</button>
</view>
</template>
<script setup>
const useCustomRef = (value : UTSJSONObject) : Ref<UTSJSONObject> => {
return customRef((track, trigger) : UTSJSONObject => {
return {
get() : UTSJSONObject {
track()
return value
},
set(newValue : UTSJSONObject) {
value = newValue
trigger()
}
}
})
}
const state = useCustomRef({ count: 0 })
const increment = () => {
(state.value as UTSJSONObject)['count'] = ((state.value as UTSJSONObject)['count'] as number) + 1
}
const triggerRefState = () => {
triggerRef(state)
}
</script>
\ No newline at end of file
......@@ -176,7 +176,7 @@
{
name: 'customRef',
url: 'custom-ref',
enable: false,
enable: true,
},
{
name: 'shallowReactive',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册