0720 - useEffect infinite loop, redux post, ag-grid checkbox, java8 stream example

1. run-p: command not found

应该是我忘了npm install…

2. How to Fix the Infinite Loop Inside “useEffect” (React Hooks)

By providing [userId] as a second argument, we are just telling “useEffect()” to run only if the certain value (userId) has changed between the component re-renders.

useEffect => (() => {
	fetchUser();
}, [userId]);  

3. Why cannot use limit in mongoDB

The LIMIT clause is not part of standard SQL. It’s supported as a vendor extension to SQL by MySQL, PostgreSQL, and SQLite.

由于我用的aqua studio自带一个输入limit的inputbox,所以我就没纠结。当然也可以尝试用mongo JS。

4. redux action post

redux action post, 把requestbody的json放在body里

5. ag-grid react checkbox rowselection

用:onSelectionChanged={onSelectionChanged}

const onSelectionChanged = () => {

	const selectedRows = gridApi.getSelectedRows();
}

6. Error: cannot deserialize from Object value

messagebodyreader not found for media type=text/plain, type=class java.lang.Exception, genericType=class java.lang.Exception

cannot construct instance of … no creators, like default construct, exist: cannot deserialize from Object value(no delegate-or property-based creator)

I got here searching for this error:

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

Nothing to do with Retrofit but if you are using Jackson this error got solved by adding a default constructor to the class throwing the error. More here: https://www.baeldung.com/jackson-exception

果然加了@AllArgsConstructor and @NoArgsConstructor然后就可以了…

7.

7.1 solve method reference warning in java8 stream:

select lambda, click alt+enter, then click extract to method reference
-> this::someFunction …

7.2 java8 stream

find max date in Object:
https://stackoverflow.com/questions/20995664/how-to-find-max-date-in-listobject

sum of values for each key:
https://stackoverflow.com/questions/51428490/java-8-stream-mapstring-liststring-sum-of-values-for-each-key

我的实现:
java stream filter example:

public String getDate(Document document) {
	// document.getFields() is List
	return document.getFields().stream()
		.filter(field -> field.getName().equals("doc_date"))
		.map(field -> field.getValue().get(0))
		.collect(Collectors.toList())
		.get(0).toString();
}

java stream groupBy and pick Object with max value example:

// documents is List
// custom key for stream groupBy
Funcion<Document, List<String>> createCustomKey = 
	document -> Arrays.<String>asList(document.getId(), document.getName());

// groupBy by custom key
Map<List<String>, List<Document>> documentsGroupByCustomKey = documents.stream().collect(Collectors.groupingBy(createCustomKey, Collectors.toList()));

List<Document> latestDocuments = new ArrayList<>();

for (Map.Entry<List<String>, List<Document>> entry : documentsGroupByCustomKey.entrySet()) {
	// get Object with max date
	latestDocuments.add(Collections.max(entry.getValue(), Comparator.comparing(this::getDate)));
}

你可能感兴趣的:(One,week,Learning,Notes,Solved,react,stream)