How can I return an undefined value with useMemo? - react-hooks

I'm writing a custom React hook, and sometimes the values returned are undefined, sometimes not.
When I return them, no problems. When I want to return them using useMemo, it doesn't work.
What I'm doing wrong ?
export function useCryptoDevs() {
(...)
const { data } = useContractReads((...))
const numberOfNft = data?.[0]
const maxNft = data?.[1]
const presaleStarted = data?.[2]
const presaleEnded = data?.[3]
// this doesn't work :/
// return (useMemo(() => { numberOfNft, maxNft, presaleStarted, presaleEnded }, [numberOfNft, maxNft, presaleStarted, presaleEnded]))
// this work, returning undefined serverside and the values clientside
return ({ numberOfNft, maxNft, presaleStarted, presaleEnded })
}

Solution
return (useMemo(() => ({ numberOfNft, maxNft, presaleStarted, presaleEnded }), [numberOfNft, maxNft, presaleStarted, presaleEnded]))
Also I would suggest to add some kind of fallback if data are undefined and then check this value if needed, eg.:
const maxNft = data?.[1] || -1 //or some other "extreme" value
Explanation
First of all, good to know is that these two are equivalent:
() => (...) //Implicit return
() => {return ...} //Explicit return
The first parameter in useMemo hook is expected to be a function, so you have three options:
useMemo(function hi() {return "Hello World"})
useMemo(() => ("Hello World"))
useMemo(() => {return "Hello World"})
Parentheses in arrow functions are (mostly) used to return an object (because all primitives and variables are allowed to be used without parentheses, so the black sheep of the family is just the object)
useMemo(() => 8) //implicit return | single-line
useMemo(() => (8)) //implicit return | multi-line
useMemo(() => {return 8}) //explicit return
//instead of 8, there could be any of the primitives, e.g.: string "Hello World"
const foo = 8;
useMemo(() => foo)
useMemo(() => (foo))
useMemo(() => {return foo})
const bar = {intval: 8, strval: "Hello World"};
useMemo(() => bar)
useMemo(() => (bar))
useMemo(() => {return bar})
//we are returning a variable here, not bare object
But to the interpreter the () => { maxNft, numberOfNft } looks like function with explicit return that I mentioned above: () => { return ... }
So you just have to make it clear that it is an object, some specific returned value, not an function with explicit return
useMemo(() => ({ maxNft, numberOfNft }), []) //implicit return of object
useMemo(() => {return { maxNft, numberOfNft }}, []) //explicit return of object

Related

JS Cypress: unable to use alias for array

I am quite new to Cypress and I have some before() calling commands that create bunch of things via API calls and return the IDs of created which I use in the after() for removing them, but somehow it works perfectly if I only return one ID and store in the alias but will fail if I store an array of IDs in alias, is this intended or I did something wrong.
in my code:
before(() => {
cy.setupEnv()
.as('access_token')
.then((token) => cy.setupFlow(token).as('data_id'))
})
after(function () {
console.log(this.access_token)
console.log(this.data_id)
})
console.log(this.data_id) shows fine if setupFlow returns only one ID but becomes undefined if I try to return [id1,id2,id3]and store the array using .as("data_id")
You've struck a strange issue, worth raising with Cypress.
It only seems to happen if you have more than one test.
For example, if I run the following it logs the array.
before(() => {
cy.wrap(1).as('access_token')
cy.then(() => {
return [1,2,3]
}).as('data_id')
})
after(function () {
console.log(this.access_token) // 1
console.log(this.data_id) // [1,2,3]
})
it('test1', () => {
console.log('test1')
expect(true).to.eq(true)
})
If I add a test it logs undefined!
before(() => {
cy.wrap(1).as('access_token')
cy.then(() => {
return [1,2,3]
}).as('data_id')
})
after(function () {
console.log(this.access_token) // 1
console.log(this.data_id) // undefined
})
it('test1', () => {
console.log('test1')
expect(true).to.eq(true)
})
it('test2', () => {
console.log('test2')
expect(true).to.eq(true)
})
One way around this is to use Cypress.env() instead
before(() => {
cy.wrap(1).as('access_token')
cy.then(() => {
Cypress.env('data_id', [1,2,3])
return [1,2,3]
}).as('data_id')
console.log('before')
})
after(function () {
console.log(this.access_token) // 1
console.log(this.data_id) // undefined
console.log(Cypress.env('data_id')) // [1,2,3]
})
beforeEach(function() {
console.log(cy.state())
console.log(this.data_id)
cy.wrap(this.data_id).as('data_id')
})
it('test1', () => {
expect(true).to.eq(true)
console.log('test1')
})
it('test2', () => {
console.log('test2')
expect(true).to.eq(true)
})
Assuming that cy.setupFlow(token) generates an array of values something like [id1, id2, id3]. This will work even when there is one value in the array. You after each should look this:
after(function () {
cy.get('#data_id').then((data_id) => {
//Get individual values
cy.log(data_id[0])
cy.log(data_id[1])
cy.log(data_id[2])
//Get all values using forEach
data_id.forEach((id) => {
cy.log(id) //get all values one by one
})
})
})
I created a small POC for this and it is working as expected.Below are the results.
Code:
describe('SO Ques', () => {
before(function () {
cy.wrap([1, 2, 3]).as('array')
})
it('SO Ques', function () {
cy.log('Hello')
})
after(function () {
cy.get('#array').then((array) => {
cy.log(array[0])
cy.log(array[1])
cy.log(array[2])
})
})
})
Result:

Jest assert promise resolved with object containing

Say I want to test a module that returns a Promise:
function myFunc () {
return Promise.resolve({
anArray: [1,2,3,4,5,6]
})
}
Using Jest, how can I assert the length of the array contained in the object the promise resolves to?
describe('myFunc', () => {
it('returns array of length 6', () => {
expect.assertions(1)
return expect(myFunc()).resolves // ... something here
})
})
If it were synchronous, I would do something like:
let result = myFunc()
expect(result.anArray.length).toBe(6)
How does this work with Promises?
There are two ways either return the promise from the test and make the assertion in the then or make your test using async/await
describe('myFunc', () => {
it('returns array of length 6', () => {
expect.assertions(1)
return expect(myFunc())
.then(result => expect(result).toEqual([1,2,3,4,5,6]);)
})
})
describe('myFunc',() => {
it('returns array of length 6', async() => {
const result = await expect(myFunc())
expect(result).toEqual([1,2,3,4,5,6]);)
})
})
The docs on this topic
The easiest approach is to use .resolves like you were starting to do in your sample.
You just need to chain .toMatchObject to the result:
function myFunc () {
return Promise.resolve({
anArray: [1,2,3,4,5,6]
})
}
describe('myFunc', () => {
it('returns array of length 6', () => {
expect(myFunc()).resolves.toMatchObject({ anArray: [1,2,3,4,5,6] }); // Success!
})
})
This will assert that the object has at least the anArray property set to [1,2,3,4,5,6] (it can have other properties as well).
Note that PR 5364 makes it so resolves validates its arguments synchronously so you don't even have to return, await, or use done if you are using Jest >= v22.2.0.
Update
Sounds like the goal is to only assert on the length of the array.
For that you would need to get the result of the Promise (as has been described in the previous answers), then use .toHaveLength to assert the length of the anArray property:
describe('myFunc', () => {
it('returns array of length 6', async () => {
const result = await myFunc();
expect(result.anArray).toHaveLength(6); // Success!
})
})
A way to do this is to pass a done callback, to mark your test as asynchronous and force jest to wait until you call done():
describe('myFunc', () => {
it('returns array of length 6', (done) => {
expect.assertions(1)
myFunc().then((values) => {
expect(values).toEqual([1,2,3...]);
done();
});
})
})
You can just return a Promise as well, without the need for done:
describe('myFunc', () => {
it('returns array of length 6', () => {
expect.assertions(1)
return myFunc().then((values) => {
expect(values).toEqual([1,2,3...]);
});
})
})
You can read more about this here.

how to access previous mergeMap values from rxjs

I am learning to use RXJS. In this scenario, I am chaining a few async requests using rxjs. At the last mergeMap, I'd like to have access to the first mergeMap's params. I have explored the option using Global or withLatest, but neither options seem to be the right fit here.
const arraySrc$ = from(gauges).pipe(
mergeMap(gauge => {
return readCSVFile(gauge.id);
}),
mergeMap((csvStr: any) => readStringToArray(csvStr.data)),
map((array: string[][]) => transposeArray(array)),
mergeMap((array: number[][]) => forkJoin(uploadToDB(array, gauge.id))),
catchError(error => of(`Bad Promise: ${error}`))
);
readCSVFile is an async request which returns an observable to read CSV from a remote server.
readStringToArray is another async request which returns an observable to convert string to Arrays
transposeArray just does the transpose
uploadToDB is async DB request, which needs gague.id from the first mergeMap.
How do I get that? It would be great to take some advice on why the way I am doing it is bad.
For now, I am just passing the ID layer by layer, but it doesn't feel to be correct.
const arraySrc$ = from(gauges).pipe(
mergeMap(gauge => readCSVFile(gauge.id)),
mergeMap(({ data, gaugeId }: any) => readStringToArray(data, gaugeId)),
map(({ data, gaugeId }) => transposeArray(data, gaugeId)),
mergeMap(({ data, gaugeId }) => uploadToDB(data, gaugeId)),
catchError(error => of(`Bad Promise: ${error}`))
);
Why don't you do simply this?
const arraySrc$ = from(gauges).pipe(
mergeMap(gauge => readCSVFile(gauge.id).pipe(
mergeMap((csvStr: any) => readStringToArray(csvStr.data)),
map((array: string[][]) => transposeArray(array)),
mergeMap((array: number[][]) => forkJoin(uploadToDB(array, gauge.id)))
)),
catchError(error => of(`Bad Promise: ${error}`))
);
You can also wrap the inner observable in a function:
uploadCSVFilesFromGaugeID(gaugeID): Observable<void> {
return readCSVFile(gaugeID).pipe(
mergeMap((csvStr: any) => readStringToArray(csvStr.data)),
map((array: string[][]) => transposeArray(array)),
mergeMap((array: number[][]) => forkJoin(uploadToDB(array, gaugeID))
);
}
In order to do this at the end:
const arraySrc$ = from(gauges).pipe(
mergeMap(gauge => uploadCSVFileFromGaugeID(gauge.id)),
catchError(error => of(`Bad Promise: ${error}`))
);
MergeMap requires all observable inputs; else, previous values may be returned.
It is a difficult job to concatenate and display the merging response. But here is a straightforward example I made so you can have a better idea. How do we easily perform sophisticated merging.
async playWithBbservable() {
const observable1 = new Observable((subscriber) => {
subscriber.next(this.test1());
});
const observable2 = new Observable((subscriber) => {
subscriber.next(this.test2());
});
const observable3 = new Observable((subscriber) => {
setTimeout(() => {
subscriber.next(this.test3());
subscriber.complete();
}, 1000);
});
console.log('just before subscribe');
let result = observable1.pipe(
mergeMap((val: any) => {
return observable2.pipe(
mergeMap((val2: any) => {
return observable3.pipe(
map((val3: any) => {
console.log(`${val} ${val2} ${val3}`);
})
);
})
);
})
);
result.subscribe({
next(x) {
console.log('got value ' + x);
},
error(err) {
console.error('something wrong occurred: ' + err);
},
complete() {
console.log('done');
},
});
console.log('just after subscribe');
}
test1() {
return 'ABC';
}
test2() {
return 'PQR';
}
test3() {
return 'ZYX';
}

How to pass action data downstream a pipeable operator stream in rxjs?

I have a situation where I want to access action payload in a third level operation.
I was able to such thing in lettable operators but how can I do the same with pipeable operator?
this my code,
#Effect()
onTrySignin = this.actions$.pipe(
ofType(AuthActions.TRY_SIGNIN),
map((action: AuthActions.TrySignin) => {
return action.payload;
}),
switchMap(action => {
return this.httpService
.postRequest('UserAccounts/Login', action.credentials);
}), catchError((error: HttpErrorResponse) => {
return Observable.of(new AuthActions.FailedAuth(error));
}),
mergeMap((response: any) => {
// how to access action payload here?
})
);
You can use map() to pass data along an observable chain like this:
// both foo and bar will be available on next()
from(AsyncFooData()).pipe(
concatMap(foo => AsyncBarData().pipe(
map(bar => ({foo, bar})
)),
tap(val => console.log(val), // chain more operators here...
).subscribe(({foo, bar}) => {
// do stuff with foo and bar
})
FWIW, I took this answer from this question where I posted a somewhat similar answer.
ok, its a pipe inside a pipe
#Effect()
onTrySignin = this.actions$.pipe(
ofType(AuthActions.TRY_SIGNIN),
map((action: AuthActions.TrySignin) => {
return action.payload;
}),
switchMap(actionPayload => {
return this.httpService.postRequest('UserAccounts/Login', actionPayload.credentials).pipe(
mergeMap((response: HttpResponse<IApiResponder<string>>) => {
switch (response.status) {
case 200:
if (actionPayload.returnUrl) {
this.router.navigate([actionPayload.returnUrl]);
} else {
this.router.navigate(['/dbapp']);
}
return Observable.concat(
Observable.of(new AuthActions.GenerateAntiforgeryToken()),
Observable.of(new AuthActions.Signin(this.authService.getUserData())),
);
}
}),
catchError(e => {
return Observable.of(new AuthActions.FailedAuth(e));
}),
);
}),
);

Observable.bindCallback only returns value if I subscribe to it directly

Observable.bindCallback only returns value if I subscribe to it directly
in other words, this works fine:
return this.store.select(store => store.appDb.appBaseUrl)
.take(1)
.mergeMap(baseUrl => {
const url = `${baseUrl}?command=GetCustomers&resellerUserName=aaa&resellerPassword=bbbb`;
return this.http.get(url)
.map(res => {
var xmlData = res.text()
const boundCallback = Observable.bindCallback(this.processXml, (xmlData: any) => xmlData);
return boundCallback(this, xmlData)
.subscribe((x) => {
return x;
});
})
})
however I need to avoid the subscription as I am running inside #effect which auto subscribes for me, so I run:
return this.store.select(store => store.appDb.appBaseUrl)
.take(1)
.mergeMap(baseUrl => {
const url = `${baseUrl}?command=GetCustomers&resellerUserName=aaa&resellerPassword=aaa`;
return this.http.get(url)
.map(res => {
var xmlData = res.text()
const boundCallback = Observable.bindCallback(this.processXml, (xmlData: any) => xmlData);
var d:any = boundCallback(this, xmlData)
return d;
}).map(d=>{console.log(d)})
})
but instead of getting a value now I am getting a:
and this is d:
regards
Sean
If I understand what you want to do it should look something like this (obviously I didn't test it):
return this.store.select(store => store.appDb.appBaseUrl)
.take(1)
.mergeMap(baseUrl => {
const url = `${baseUrl}?command=GetCustomers&resellerUserName=aaa&resellerPassword=aaa`;
return this.http.get(url)
.mergeMap(res => {
var xmlData = res.text()
const boundCallback = Observable.bindCallback(this.processXml, (xmlData: any) => xmlData);
return boundCallback(this, xmlData)
}).do(d => console.log(d))
})
I used mergeMap() because I want to get the value from the Observable returned by boundCallback().
Also when using map() you always need to return a value that is propagated further. In your example you're not returning anything so you can use just do() to see print what values go through.
Edit:
So this is a simplified version of what you're trying to do.
class A {
private processXml(context, xmlData, cb) {
context.parseString(xmlData, {attrkey: 'attr'}, function (err, result) {
if (err || !result) return cb(null); return cb(result);
})
}
private parseString(str, _, cb) {
return cb(null, str);
}
private mockHttpGet() {
return Rx.Observable.of({
text: () => {
return 'abc';
}
});
}
test() {
return this.mockHttpGet()
.mergeMap(res => {
var xmlData = res.text();
const boundCallback = Rx.Observable.bindCallback(this.processXml, (xmlData: any) => xmlData);
return boundCallback(this, xmlData)
}).do(d => console.log(d))
}
}
let a = new A();
a.test().subscribe(val => console.log('subscribed result', val));
See live demo: https://jsbin.com/reweraw/2/edit?js,console
This demo prints:
abc
subscribe abc
The BoundCallbackObservable (and this applies to operators as well) do nothing until you subscribe to them. That's why in the debugger you see just raw data.
My demo works as you probably want so check out how am I using mergeMap() to get the actual value from the nested Observable and try to replicate the same logic in you application.
found the solution, I was one Obs too deep:
return this.store.select(store => store.appDb.appBaseUrl)
.take(1)
.mergeMap(baseUrl => {
const url = `${baseUrl}?command=GetCustomers&resellerUserName=reseller#ms.com&resellerPassword=XXXX`;
return this.http.get(url)
.map(res => {
return res.text()
}).flatMap(jData=>{
const boundCallback = Observable.bindCallback(this.processXml, (xmlData: any) => xmlData);
return boundCallback(this, jData)
}).map(businessData=>{
return businessData;
})
})

Resources