如果用户经过身份验证并可以通过React Context API访问它,我想保存一个简单的布尔值 + userID 。
许多指南用上下文提供者包装了他们的根组件。对我来说,包装整个应用程序似乎很浪费。另一方面,我需要在我的所有主页中提供此信息。
用 React Context Provider 包装整个应用程序会产生负面影响吗?
例子:
class App extends Component {
render() {
return (
<MyProvider>
<div className="App">
<h1 className="App-title">Welcome to my web store</h1>
<ProductList />
</div>
</MyProvider>
);
}
Run Code Online (Sandbox Code Playgroud)
}
我使用本指南作为参考。
我没有任何 React Redux 经验,所以这对于以前使用过这个框架的每个人来说可能都很自然。
研究 Google 后发现了许多关于如何实现 React Context 或使用 HOC 的指南,但我点击的 15 个指南并没有回答我的问题。
我将Gatsby用作静态网站生成器。我使用React Context API来存储用户已通过身份验证的信息。
在开发模式下,当我键入任何重定向到404错误页面的URL时,上下文数据将丢失。当我导航到有效页面时,以前登录的用户不再登录。
我之前从未使用过React Context,所以不确定如何处理。那是预期的行为吗?有什么方法可以保留用户特定的React Context?我是否需要从后端重新提供上下文?还是我只是犯了一个错误?最好的行动方针是什么?
我想以某种方式将上下文保留在浏览器中,直到会话超时。尚未执行来自后端的会话处理。
编辑:我刚刚用gatsby构建和gatsby服务测试了这一点。当重定向到404错误页面时,内置的gatsby网站会保留上下文。但是,当导航到完全不同的URL(例如www.google.com)时,上下文仍然丢失。
现在我的问题是:如何在不让用户再次手动登录的情况下为上下文提供登录信息?Cookie检查?抱歉,如果我要问一个明显的问题。我从未实现过会话或cookie。
这是我的AuthContextProvider包装器类:
import React from "react";
export const AuthContext = React.createContext();
export class AuthContextProvider extends React.Component {
state = {
authenticated: false,
toggleLogin: () => {},
userid: null,
};
render() {
return (
<AuthContext.Provider
value={{
authenticated: this.state.authenticated,
userid: this.state.userid,
toggleLogin: () => {
const previousValueState = this.state.authenticated;
this.setState(state => ({
authenticated: !previousValueState,
userid: 2,
}));
},
}}
>
{this.props.children}
</AuthContext.Provider>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我用Context Provider将整个应用程序包装在根布局中:
export default function RootLayout({ …
Run Code Online (Sandbox Code Playgroud) 我在 Netlify 上托管了一个 Gatsby 站点。当我部署一个新版本时,我对站点所做的更改在我刷新页面之前是不可见的。有谁知道为什么会这样?
我正在使用gatsby-plugin-offline
& gatsby-plugin-manifest
。我看到我可以安装gatsby-plugin-remove-serviceworker
插件来删除服务工作者,但如果可能的话,我想继续使用它。
这是一个很难搜索的问题,因为我得到了有关.cache
本地目录中文件夹的结果
我正在尝试从 Jenkins 声明式管道执行curl post 命令,但是,它抛出语法错误 - Expecting '}' find ':'
管道脚本如下:
pipeline {
agent { label ' Linux01'}
stages {
stage('Hello') {
steps {
sh 'curl -u username:password -X POST -d '{"body":"Jenkinspipleinecomment"}' -H "Content-Type:application/json" http://localhost:8080/rest/api/2/issue/someissue/comment'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙。
我有一张包含公司名称的表格。由于人为输入错误,存在许多重复项。对于是否应包含细分、拼写错误等存在不同的看法。我希望所有这些重复项都被标记为一个公司“1c”:
+------------------+
| company |
+------------------+
| 1c |
| 1c company |
| 1c game studios |
| 1c wireless |
| 1c-avalon |
| 1c-softclub |
| 1c: maddox games |
| 1c:inoco |
| 1cc games |
+------------------+
Run Code Online (Sandbox Code Playgroud)
我认为编辑距离是消除拼写错误的好方法。然而,当添加细分时,编辑距离会急剧增加,并且不再是一个好的算法。它是否正确?
总的来说,我在计算语言学方面几乎没有任何经验,所以我不知道应该选择什么方法。
对于这个问题你会推荐什么算法?我想用java实现它。纯 SQL 也可以。来源链接将不胜感激。谢谢。
我将道具传递给按钮组件:
const StoreButton = ({ storeColor }) => {
const borderBottom = `solid 3px ${storeColor}`;
const classes = useStyles({ borderBottom });
return (
<Button variant="outlined" size="small" className={classes.button}>
Store
</ Button>
)
};
Run Code Online (Sandbox Code Playgroud)
我borderBottom
在类中使用它之前创建了该属性。我想在 makeStyles 中构造属性,但这会导致错误:
const useStyles = makeStyles(theme => ({
button: {
borderBottom: props => props.borderBottom,
// borderBottom: `solid 3px ${props => props.borderBottom}`; // function is pasted in
},
}));
Run Code Online (Sandbox Code Playgroud)
如果我在 makeStyles 中构造 CSS 速记,它会创建solid 3px solid 3px function(props){return props.borderBottom;}
. 当我在 Chrome 中检查它时。因此,这种方式的样式是无效的。
有没有办法将 props …
我想通过 GitHub REST API 设置存储库机密。我使用文档中的示例:
const sodium = require('tweetsodium');
const key = "base64-encoded-public-key";
const value = "plain-text-secret";
// Convert the message and key to Uint8Array's (Buffer implements that interface)
const messageBytes = Buffer.from(value);
const keyBytes = Buffer.from(key, 'base64');
// Encrypt using LibSodium.
const encryptedBytes = sodium.seal(messageBytes, keyBytes);
// Base64 the encrypted secret
const encrypted = Buffer.from(encryptedBytes).toString('base64');
console.log(encrypted);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
(node:6008) UnhandledPromiseRejectionWarning: Error: bad public key size
at checkBoxLengths (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2158:54)
at Function.nacl.box.before (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2231:3)
at Object.nacl.box (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2225:20)
at Object.tweetSodium.seal (C:\Users\User\probot\node_modules\tweetsodium\dist\index.umd.js:53:33)
at …
Run Code Online (Sandbox Code Playgroud) reactjs ×3
gatsby ×2
javascript ×2
caching ×1
css ×1
curl ×1
duplicates ×1
encryption ×1
github ×1
jenkins ×1
jenkins-declarative-pipeline ×1
jira ×1
jss ×1
libsodium ×1
linguistics ×1
material-ui ×1
netlify ×1
nlp ×1
node.js ×1
static-site ×1