我很好奇为什么const构件可以在构造函数中修改.
初始化中是否有任何标准规则可以覆盖成员的"常量"?
struct Bar {
const int b = 5; // default member initialization
Bar(int c):b(c) {}
};
Bar *b = new Bar(2); // Problem: Bar::b is modified to 2
// was expecting it to be an error
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
一些std::optional
构造函数使用这样的std::in_place_t
标记参数:
template< class... Args >
explicit optional( std::in_place_t, Args&&... args );
Run Code Online (Sandbox Code Playgroud)
我看到这样的构造函数可以在没有就地标记的情况下实现,并使用一些enable-if魔法不参与不愿意的重载,即简单地说:
template< class... Args >
explicit optional( Args&&... args );
Run Code Online (Sandbox Code Playgroud)
为什么std::optional
使用就地标记实现就地构造函数而不是一些启用魔法(并且没有标记)?
更新:问题略有更新,以强调我意识到简单地省略就地标记是行不通的.
我正在使用ReactJS并aws-amplify
执行graphql操作.
码:
import {
API,
graphqlOperation
} from 'aws-amplify';
import { UpdateInput } from './mutations.js';
// Call mutation
const input = { /* some values */ };
API.graphql(graphqlOperation(UpdateInput, input)).then(...);
Run Code Online (Sandbox Code Playgroud)
GraphQL变异定义:
export const UpdateInput = `mutation UpdateInput($input: Input!) {
updateInput(input: $input) {
id,
name
}
}`
Run Code Online (Sandbox Code Playgroud)
GraphQL架构:
input Input {
id: ID!
name: String
}
type Mutation {
updateInput(input: Input!): String
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
[Log]变量'input'强制NonNull类型'Input!'的Null值
使用AWS控制台我的突变工作并且input
是NonNull(使用调试器)
是什么导致错误?
我无法弄清楚“useSelector”发生了什么,我几乎不需要帮助。
错误
React Hook "useSelector" 在函数 "render_user" 中调用,该函数既不是 React 函数组件也不是自定义 React Hook 函数
class Navigationbar extends Component {
onLogoutClick = e => {
e.preventDefault();
this.props.logoutUser(); //this.props.
};
render() {
const render_user = () => {
const auth = useSelector(state => state.auth); Error Message is here
//More Code Here
);
};
}
Run Code Online (Sandbox Code Playgroud)
Navigationbar.propTypes = {
logoutUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(
mapStateToProps,
{ logoutUser }
)(Navigationbar);
Run Code Online (Sandbox Code Playgroud) 在这里反应钩子菜鸟......
鉴于这个例子
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
// Specify how to clean up after this effect:
return function cleanup() {
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
});
Run Code Online (Sandbox Code Playgroud)
从文档
当组件卸载时,React 执行清理。但是,正如我们之前所了解的,效果在每次渲染时都会运行,而不仅仅是一次。这就是为什么 React 还会在下次运行效果之前清除上一次渲染中的效果。
这是否意味着unsubscribeFromFriendStatus
仅在组件卸载或每次渲染时运行一次?
扩展我的问题:
ifunsubscribeFromFriendStatus
每次都运行,而跳过它的唯一方法是使用可选的第二个参数……那么实现componentWillMount
and的原始显式执行是不是更难了componentWillUnmount
?说,我想subscribe
什么时候componentWillMount
,只unsubscribe
在componentWillUnMount
什么时候运行?
我有一个问题,即Google Translate API的翻译文本中没有换行符.
我有一个像这样的原始查询字符串:
RELATED WORK .
Studies of group work have shown the importance of
Run Code Online (Sandbox Code Playgroud)
我为查询字符串做了一个URL编码,它显示了这个:
RELATED%20WORK%20.%0D%0A%0D%0AStudies%20of%20group%20work%20have%20shown%20the%20importance%20of
Run Code Online (Sandbox Code Playgroud)
问题是提交给Google Translate API时:
https://www.googleapis.com/language/translate/v2?key=<key>&source=en&target=ja&q=RELATED%20WORK%20.%0D%0A%0D%0AStudies%20of%20group%20work%20have%20shown%20the%20importance%20of
Run Code Online (Sandbox Code Playgroud)
我只在一行中得到响应(没有换行符):
{
"data": {
"translations": [
{
"translatedText": "???? ?????????????"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是逐行解析翻译的文本以进行正确的渲染.
我只是通过浏览器访问它来显示URL,它不显示响应中的换行符.
有任何想法吗?
我只是好奇是否可以在 Context API 中使用 Context API。例如,我将为 AppState 提供一个 Context API,并希望在另一个处理 WebSocket 连接的 Context API 中使用它?
我正在尝试在窗口调整大小事件上测量 React DOM 节点。我在React hooks-faq上使用过这个例子,但它只发生在第一次渲染中。如果我添加一个useEffect
来监听调整大小,回调不会被调用?
function MeasureExample() {
const [height, setHeight] = useState(0);
const measuredRef = useCallback(node => {
if (node !== null) {
setHeight(node.getBoundingClientRect().height);
}
}, []);
// Adding this effect doesn't re-calculate the height???
useEffect(() => {
window.addEventListener("resize", measuredRef);
return (): void => {
window.removeEventListener("resize", measuredRef);
};
}, [height])
return (
<>
<h1 ref={measuredRef}>Hello, world</h1>
<h2>The above header is {Math.round(height)}px tall</h2>
</>
);
}
Run Code Online (Sandbox Code Playgroud) 因此,我将使用非常基本的组件进入Reactjs。我从不同的函数中注销了相同的状态,但是看到的是不同的值。
import React, { useState, useEffect, useRef } from "react";
const Test = props => {
const [count, setCount] = useState(0);
useEffect(()=>{
setInterval(() => {
console.log("count in interval is:", count);
}, 1000);
},[props]);
function btnClick() {
const newCount = count + 1;
setCount(newCount);
console.log("count changed to: ", newCount);
}
return (
<div>
count is {count}
<br></br>
<button onClick={btnClick}>+</button>
</div>
);
};
export default Test;
Run Code Online (Sandbox Code Playgroud)
单击几下后输出,日志为:
Test.js:8 count in interval is: 0
Test.js:15 count changed to: 1
Test.js:15 count changed to: …
Run Code Online (Sandbox Code Playgroud) 我一直在使用默认构造函数并注意到一个奇怪的行为(从我的角度来看)。
当我声明时A() = default
,我没有收到链接器错误。
struct A
{
int a;
A() = default;
};
A a; // no linker error
Run Code Online (Sandbox Code Playgroud)
然而,当我宣布A();
我明白了。
struct A
{
int a;
A();
};
A a; // linker error - undefined reference to `A::A()`
Run Code Online (Sandbox Code Playgroud)
问题:
两者有什么区别?
如果A();
产生链接器错误,为什么首先支持它?有什么实际应用吗?
更新(后续问题)
A();
如果用户没有指定定义,为什么编译器不能隐式定义它?
reactjs ×6
javascript ×4
c++ ×3
react-hooks ×3
c++11 ×2
c++14 ×2
aws-amplify ×1
aws-appsync ×1
c++17 ×1
const ×1
graphql ×1
line-breaks ×1
react-native ×1
react-redux ×1
stdoptional ×1
websocket ×1