我有一个 useEffect
function Component () {
const facets = useSelector(getFacets)
useEffect(() => {
dispatch(search(facets, param2, param3))
}, [dispatch, facets, param2, param3])
//rest of component with return
}
Run Code Online (Sandbox Code Playgroud)
现在搜索实际上会更新facets
//Inside my slice
const search = (facets, param2, param3) => async(dispatch) => {
//do a fetch using param2 and param3
//fetch comes back with a facets object
//alter response facets object based on 'old' facets
dispatch(setFacets(newFacets)) // set the new facets
}
Run Code Online (Sandbox Code Playgroud)
问题是,当此 useEffect 运行时,重新渲染会导致const facets = useSelector(getFacets)加载新值... Linting …
我试图将一个简单的if / else语句转换为三元语句以进行练习,但遇到了麻烦。据我了解,逻辑是:
condition ? (action to take if condition is true) : (action if false);
Run Code Online (Sandbox Code Playgroud)
我的情况是if(result == 8)。
我已经尝试过:
result == 8 ? return true : return false;
Run Code Online (Sandbox Code Playgroud)
和
result = 8 ? return true : return false;
Run Code Online (Sandbox Code Playgroud)
这是我的代码,我想将其结尾转换为
public boolean sum28(int[] nums) {
int result = 0;
for(int i=0; i<nums.length; i++) {
if(nums[i] == 2) {
result+=2;
}
}
if(result == 8) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我遇到类型不匹配的情况:当我仅使用1个等号时,无法从int转换为boolean,而当使用两个等号时,则得到了无效的令牌“ ==”。
初始字符串:
initString = '/digital/collection/music/bunch/of/other/stuff'
我想要的是: music
collection/和之间的任何术语(绝不包含斜杠)/bunch我要怎么做:
if(initString.includes('/digital/collection/')){
let slicedString = initString.slice(19); //results in 'music/bunch/of/other/stuff'
let indexOfSlash = slicedString.indexOf('/'); //results, in this case, to 5
let desiredString = slicedString.slice(0, indexOfSlash); //results in 'music'
}
Run Code Online (Sandbox Code Playgroud)
问题:
如何通过javascript更好地完成此操作?
endIndexOf()来代替我的硬编码.slice(19)
lastIndexOf() 不是我想要的,因为我希望索引在我的子字符串的第一个实例的末尾 /digital/collection/.getStringBetween('beginCutoff, endCutoff') 先感谢您!