我有一个飞行状态,我有一个滑块可以更改最高价格以更改飞行元素的可见性.
maxpriceFilter() {
var flightOffer = this.state.flightOffer;
var sliderPrice = this.state.sliderPrice;
for (var i = 0; i < flightOffer.length; i++) {
if( flightOffer[i].price > sliderPrice) {
this.setState(
{[flightOffer[i].hiddenprice] : true}
);
};
}
Run Code Online (Sandbox Code Playgroud)
这段代码在状态的根目录中添加了一个状态为true的未定义字段.我无法找到任何最佳实践,除此之外使用计算字段.但我不能让计算字段工作..
有人可以帮帮我吗?
努力获取滑块以更改 React 状态中“文本”的值。
一直报错:
“App.js:90 Uncaught TypeError: this.setState is not a function”尽管我尽了最大努力进行故障排除。
解决办法是什么?
class App extends Component {
constructor(props) {
super(props);
this.state = {list: [{x: "Before Pool", y:85000}, {x: "After Pool", y:82000}], text: 0, options: {bathrooms:'', bedrooms:'', sqft:''}};
}
componentDidMount() {
setTimeout(() => {
this.setState({list: [{x: "Before Pool", y:60000}, {x: "After Pool", y:30000}]});
console.log("testing", this.state.text);
}, 2000) ;
}
handleChange (event) {
console.log("from handle change", event);
this.setState({text : event });
}
render() {
return (
<div className="App">
<div>
<div style={wrapperStyle}> …Run Code Online (Sandbox Code Playgroud) 如何通过setState更改getInitialState中的对象中的项?
例如:
getInitialState: function() {
return {list: [
{id:'1',
title: 'The Baby Boss',
year: '2017',
quality: 'Blu-ray',
stars: ['Fred Astaire, ', 'Humphrey Bogart, ', 'Marlon Brando, ', 'Richard Burton, ', 'Charlie Chaplin'],
Show: false
},
{
id:'2',
title: 'Pirates of the Caribbean: Dead Men Tell No Tales ',
year: '2016',
quality: 'HDrip',
stars: ['John Depp, ', 'Orlando Bloom, ', 'Javier Bardem, ', 'Kaya Scodelario, ', 'Brenton Thwaites'],
Show: true
}`
Run Code Online (Sandbox Code Playgroud)
更清晰.我已创建getInitialState并render编辑它,并在屏幕上显示列表对象.我有按钮,我想点击它并改变其中一个对象的smth.通过列表中的项目我使用'map'功能.结果,我试图像下面的代码一样做:
this.setState({
film.Show: false
})
Run Code Online (Sandbox Code Playgroud)
但这种方式不起作用.达到我的目标的哪种方式是正确的?感谢您的想法.
我目前的状态是这样的:
state = { items : [{id: 1, text: 'test words'}, {id: 2, text: 'another test'}]
Run Code Online (Sandbox Code Playgroud)
这是我从阵列中删除对象的功能,试图避免改变状态。
handleRemove(passedInId) {
const myItems = this.state.items
const newArray = myItems.filter(item => item.id !== passedInId)
this.setState(prevState => ({
items: prevState.items = newArray
}))
console.log('handle remove runned', passedInId, myItems, newArray)
}
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,但是在继续生活之前想知道它是否不是反模式
非常感谢!!
this.setState(prevState => ({
score: prevState.score + 10,
rightAnswers: prevState.rightAnswers + 1,
currentQuestion: setTimeout(() => {
prevState.currentQuestion + 1
}, 2000)
}))
}
Run Code Online (Sandbox Code Playgroud)
在按钮上单击我更改状态。我的目标是延迟currentQuestion状态更改,在此期间我想显示某些状态消息,但我想立即更新分数而没有延迟。
这样做的正确方法是什么?
PS:此变体无法正常工作,它只代表我想做的事情。
谢谢。
我有一个组件,我有一个属性的状态.构造函数如下所示:
constructor (props, context) {
super(props, context)
this.state = {
property1: '',
property2: '',
property3: '',
}
}
Run Code Online (Sandbox Code Playgroud)
说我想要更新property1,我通常会做的是这样的事情:
this.setState({property1: 'my new property1'})
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用,价值property1不会改变.要让我更新任何州,我必须做以下事情:
let obj = this.state
obj.property1 = 'my new property1'
this.setState(obj)
Run Code Online (Sandbox Code Playgroud)
我知道一个事实,以前的方式过去没有问题.有什么变化?在某些情况下,我不能简单地设定每个州的财产吗?
当我setState向_images阵列添加图像时,它似乎已经添加了,但是随后迅速还原:
这种形式大致遵循Brian Egan的redux架构示例:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class Note {
final String comments;
final List<String> images;
Note({
this.comments,
this.images,
});
}
class AddNote extends StatefulWidget {
final Note note;
final bool isEditing;
AddNote({
this.note,
this.isEditing,
});
@override
_AddNoteState createState() => _AddNoteState();
}
class _AddNoteState extends State<AddNote> {
static final _scaffoldKey = GlobalKey<ScaffoldState>();
static final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
List<String> _images;
String _comments;
Note get _note => widget.note;
bool get _isEditing => widget.isEditing; …Run Code Online (Sandbox Code Playgroud) 我做了什么 :
context和,setState()在页面的所有构建函数中调用了这两行:Run Code Online (Sandbox Code Playgroud)@override Widget build(BuildContext context) { StaticClass.currentContext = context; StaticClass.currentSetState = this.setState; return ... ; }
Run Code Online (Sandbox Code Playgroud)fcm.configure( onMessage: (){ StaticClass.currentSetState((){ Navigator.pushNamed(StaticClass.currentContext, "/notifications"); }); });
发生了什么 :
Run Code Online (Sandbox Code Playgroud)??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ???? ... setState() or markNeedsBuild() called during build. This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. ...
说明:
我想要的是 :
要么
要么 …
试图了解React中setState()的工作原理。我的问题是这个。
如果我有其中一行是setState()的函数,那么在重新渲染之前,该行之后的代码行是否仍会运行?例如,在下面的代码中,
foo(value) {
this.setState({stateValue: value});
console.log("Stuff after setState");
}
Run Code Online (Sandbox Code Playgroud)
在该代码中,console.log是否保证可以运行?它在我测试时运行,但是我不确定这是否仅是因为React还没有时间重新渲染。我可以期望this.setState()之后的代码运行吗?
我正在尝试在对象数组中映射一些字段,在这种情况下,这是字段名和排序顺序。
我试图实现服务器端排序功能,每当我单击一个字段时,服务器中的服务器都会采用字段名称和排序类型。我只需要使用排序类型(ASCENDING或DESCENDING)来映射字段名称。
我编写了一个示例,其中维护了一个带有类型的对象的示例数组。在单击该列时,我需要确定其排序顺序
有人可以帮忙吗,只需要用字段名称来实现排序的标签
沙箱:https://codesandbox.io/s/hopeful-wescoff-08x8x
import React from "react";
import ReactDOM from "react-dom";
import { render } from "react-dom";
interface IState {
sorting: any;
}
interface IProps {}
export default class App extends React.Component<IProps, IState> {
constructor(props: any) {
super(props);
this.state = {
sorting: [{ firstName: "" }, { lastName: "" }]
};
}
sortHandler = name => {
const sorting = Object.keys(this.state.sorting).reduce((obj, key) => {
if (key === name) {
obj[key] = this.state.sorting[key] === "ASC" ? …Run Code Online (Sandbox Code Playgroud) setstate ×10
reactjs ×8
javascript ×4
dart ×2
ecmascript-6 ×2
flutter ×2
firebase ×1
onchange ×1
settimeout ×1
state ×1
timedelay ×1
typescript ×1