我遇到一个错误:
超过最大更新深度。当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时,可能会发生这种情况。React限制了嵌套更新的数量,以防止无限循环。
我的代码是:
class Login extends React.Component {
constructor(props){
super(props);
this.state = {
firstName: '',
password: '',
isLogged: false
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChangeFirstName = this.handleChangeFirstName.bind(this);
this.handleChangePassword = this.handleChangePassword.bind(this);
}
handleChangeFirstName(event) {
this.setState({firstName: event.target.value})
}
handleChangePassword(event) {
this.setState({password: event.target.value})
}
handleSubmit(event) {
console.log('login');
console.log('first name:' ,this.state.firstName);
console.log('password:' ,this.state.password);
this.props.loginRequest(this.state.firstName, this.state.password);
// console.log('login',this.props)
}
componentDidUpdate() {
if(this.props.request.message === 'ok'){
console.log('ok');
this.setState({isLogged: true});
this.props.history.push('/');
//path is ok!!!
console.log('path from history: ', this.props.history);
}
}
render() {
return(
<div>
{this.state.isLogged ? <App /> …Run Code Online (Sandbox Code Playgroud) 几天来我一直面临着一件奇怪的事情,到目前为止我还没有解决这个问题。问题是我有一个 React.js 前端、Node Express.js 后端和 MongoDB,当我使用/api/users/update-pwd正确的参数发出请求时axios,它不会更新密码,但返回 200。然后,我尝试了一些路线例如/api/users/psosp。令我惊讶的是,它也返回了 200。我找不到原因。任何有用的提示都将受到高度赞赏。
Axios.post("/users/update-password",{
password: n_pwd
}).then(
res => {
alert("Password Updated")
}
).catch(err => {
console.log(err)
alert("An error occured")
})
Run Code Online (Sandbox Code Playgroud) 在我的 Express 应用程序中,使用 生成express-generator,我想在其他一些控制器文件中使用 来将数据发送到客户端套接字io。socket.io我的方法如下,但我收到以下错误。如果有人能在这种情况下帮助我,那将是一个很大的帮助。
(节点:11376)UnhandledPromiseRejectionWarning:TypeError:io.emit 不是 F:\backend\controllers\LessonController.js:169:9 处的函数
在由 生成的 Express 应用程序中express-generator,创建服务器的过程发生在/bin/www.js. 我尝试从那里导入io实例并在其他文件中使用它,但它不起作用。
bin/www.js
#!/usr/bin/env node
var app = require('../app');
var debug = require('debug')('backend:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);
var server = http.createServer(app);
const io = require('socket.io')(server);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
// several other functions are omitted for brevity
module.exports = io;
Run Code Online (Sandbox Code Playgroud)
LessonController.js
const Lesson = require('../models/Lesson');
const Course …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查数据库中是否已存在电子邮件和用户名。由于我使用的是mongoose和express.js,因此仅检查数据库中的用户名是否已存在,但是我需要检查电子邮件和用户名并为每个用户返回适当的错误消息。但是,我不知道如何检查它们。这是我到目前为止编写的一些代码:
User.findOne({
'username': username
},
function(err, user) {
if (err) {
console.log('Signup error');
return done(err);
}
//if user found.
if (user) {
console.log('Username already exists, username: ' + username);
var err = new Error();
err.status = 310;
return done(err);
}
Run Code Online (Sandbox Code Playgroud) 我有两个RestController用户和公司。
CompanyController:companies使用服务和回购级别从公司信息中获取并将其存储到表中。
UsersController : 用于获取和存储用户。
关系:每个用户都与公司相关联(用户有一个公司)。
当新人User注册时,我们需要通过 id 获取公司信息并与用户个人资料相关联。
为此,我在 中有一个端点CompanyController,名为getCompanyInfo。我必须在保存用户配置文件的同时调用该端点来获取公司数据。
如何在 Spring Boot 中从同一个应用程序调用另一个 API?
我希望在未选择选项卡时将颜色设置为默认的灰色,而在选择选项卡时将其设置为我的tabBarColor颜色。我在标签栏中找不到更改标题颜色的方法。
我怎样才能做到这一点?
这是我的代码:
Home:{
screen: TabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: ({ navigation }) => ({
title: 'Home',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-home' : 'ios-home-outline'}
size={26}
style={{ color: focused ? `${tabBarColor}` : tintColor}}
/>
),
//headerStyle: {backgroundColor: "#553A91"},
//headerTitleStyle: {color: "#FFFFFF"},
header: null,
}),
},
Profile: {
screen: ProfileScreen,
navigationOptions: ({ navigation }) => ({
title: 'Profile',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={focused ? 'ios-people' : 'ios-people-outline'}
size={26}
style={{ …Run Code Online (Sandbox Code Playgroud) 我有下面的功能,点击按钮时调用。一切正常,但document.execCommand ('copy')根本不起作用。
如果我创建另一个按钮并仅调用if单独函数中的内容,则效果很好。
我已经尝试在第一个函数中调用第二个函数,但它也不起作用。副本仅在函数中单独存在时才有效。
有谁知道发生了什么?
copyNshort = () => {
const bitly = new BitlyClient('...') // Generic Access Token bit.ly
let txt = document.getElementById('link-result')
bitly.shorten(txt.value)
.then((res) => {
this.setState({ shortedLink: res.url })
if (this.state.shortedLink !== undefined) {
document.getElementById('link-result-shorted').select() // get textarea value and select
document.execCommand('copy') // copy selected
console.log('The link has been shortened and copied to clipboard!')
ReactDOM.render(<i className="fas fa-clipboard-check"></i>, document.getElementById('copied'))
}
console.log('Shortened link ', res.url) // Shorted url
})
}
Run Code Online (Sandbox Code Playgroud) 我在我的 React Native 项目中使用 React-native-elements。我需要在卡片标题右侧添加一个“+”号,如下所示,使用react-native-vector-icons. 我浏览了文档,但找不到任何相关内容。
<Card
title="Resources"
titleStyle={{
textAlign: 'left',
}}
>
<Text style={{textAlign: "center"}}>No resources</Text>
</Card>
Run Code Online (Sandbox Code Playgroud) 我有两个组件Star和StarRating((尝试实现来自React和Redux的书中的示例,第135页)。因此,我想渲染任何星星并在单击一颗星星时执行一些操作。因此,我尝试添加console.logonClick,但是没有用。您能帮我修复代码并解释为什么它不起作用吗?
StarRating.js
import React from "react";
import Star from "./Star";
import { Component } from "react";
class StarRating extends Component {
displayName: "star-rating";
constructor(props) {
super(props);
this.state = {
totalStars: 5,
starsSelected: 2
};
this.change = this.change.bind(this);
}
change(starsSelected) {
console.log(starsSelected);
this.setState({ starsSelected });
}
render() {
const totalStars = 5;
const { starsSelected } = this.state;
console.log({ totalStars });
return (
<div className="star-rating">
{[...Array(totalStars)].map((n, i) => {
return (
<Star
key={i} …Run Code Online (Sandbox Code Playgroud) express ×3
node.js ×3
reactjs ×3
javascript ×2
react-native ×2
axios ×1
execcommand ×1
mongoose ×1
rest ×1
socket.io ×1
spring ×1
spring-boot ×1
tabnavigator ×1