So I'm trying recoilJS for a js game that I am building and it pretty neat, but the need to update atoms from ponents only feels like a limitation.
To create a game loop, I put all the logic on empty ponent so I will be able to read and write states. Even if I will construct the login outside of the ponent, I will need especially move different stats around all the time. There is a way to update atoms outside of react ponent (not via hooks)?
So I'm trying recoilJS for a js game that I am building and it pretty neat, but the need to update atoms from ponents only feels like a limitation.
To create a game loop, I put all the logic on empty ponent so I will be able to read and write states. Even if I will construct the login outside of the ponent, I will need especially move different stats around all the time. There is a way to update atoms outside of react ponent (not via hooks)?
Share Improve this question asked Jun 6, 2020 at 9:27 Smiled_OneSmiled_One 4651 gold badge4 silver badges18 bronze badges2 Answers
Reset to default 7I use RXJS to help to set RecoilJS value outside of the ponent.
At the start, I created 4 parts as
- Main ponent
- RecoilJS ponent
- Atom file
- set RecoilJS outside value of the ponent file
1).Main
import React from 'react';
import {
RecoilRoot
} from 'recoil';
function App() {
return (
<RecoilRoot>
<MainScreens />
<RecoilJSComponent/>
</RecoilRoot>
);
}
2).RecoilJS ponent
import React from 'react';
import {
useRecoilCallback
} from 'recoil';
import { Subject } from 'rxjs';
export const setRecoil = new Subject();
const getRecoil = new Subject();
const returnRecoil = new Subject();
export const promiseGetRecoil = (recoilObj) => {
return new Promise(async (resolve, reject) => {
getRecoil.next(recoilObj)
returnRecoil.subscribe({
next: (value) => {
if (recoilObj === value.recoilObj) {
resolve(value.value)
}
}
});
})
}
export default function RecoilJSComponent() {
const setStore = useRecoilCallback(({ set }) => (n) => {
set(n.recoilObj, () => (n.value));
}, [])
const getStore = useRecoilCallback(({ snapshot }) => async (recoilObj) => {
const valueRecoilObj = await snapshot.getPromise(recoilObj);
returnRecoil.next({ recoilObj: recoilObj, value: valueRecoilObj })
}, [])
setRecoil.subscribe({
next: (value) => {
setStore(value)
}
});
getRecoil.subscribe({
next: (recoilObj) => {
getStore(recoilObj)
}
});
return null;
}
3).Atom file
export const textState = atom({
key: 'textState'
default: ''
});
4).set RecoilJS outside the value of the ponent file
import API from './Api';
import { setRecoil } from './RecoilJSComponent'
import { textState } from './textState'
export const setValueReCoil = () => {
API()
.then(result => {
setRecoil({ recoilObj: textState, value: result })
})
.catch(ex => {
})
};
The main idol is in 2 and 4
In number 2, I create to use RXJS for setting value via the ponent and I export RXJS to set a value on RecoilJS outside of the ponent
I hope my idol can help you to resolve your problem
There isn't right now. Opened a suggestion for recoil team.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744195085a4562617.html
评论列表(0条)