not*_*sky 4 apollo reactjs apollo-server react-apollo apollo-client
src/another_folder/reactiveVarInitializationFile.js 从“@apollo/client”导入 {makeVar}
导出 const selectStore = makeVar('')
//我的组件
import {Select,Spin} from "antd"
import {selectStore} from "../../reactives/selectStore.RV"
import {useQuery} from "@apollo/client"
import FETCH_STORES from "../../queries/getStoresSelectSoreDPD"
export default function(){
const {data}= useQuery(FETCH_STORES)
const store = selectStore()
const onChange=(val)=>{
console.log(val)
selectStore(val)
}
console.log(store)
return(
<>
{!data?(<Spin/>):(
<Select
style={{width:"200px"}}
placeholder="Select store"
onChange={onChange}>
{data.currentUser.outlets.map(el=><Select.Option key={el.id} value={el.id}>{el.name}
</Select.Option>)}
</Select>
)}
<p>{store}</p>
</>
)
}
Run Code Online (Sandbox Code Playgroud)
问题是当我将 selectStore 导入到我的组件中时,我无法通过 selectStore() 获取其值或通过执行 selectStore("new value") 修改 var
有人可以帮我吗?
如果直接使用reactiveVar,当值改变时它不会更新,它只会在初始渲染时更新。
但是,如果您希望它在值更改时更新并触发重新渲染,则需要使用挂钩useReactiveVar: https: //www.apollographql.com/docs/react/local-state/reactive-variables/
所以,像这样:
const store = useReactiveVar(selectStore);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4072 次 |
| 最近记录: |