我试图通过使用这样的嵌套属性来组织我的状态:
this.state = {
someProperty: {
flag:true
}
}
Run Code Online (Sandbox Code Playgroud)
但是像这样更新状态,
this.setState({ someProperty.flag: false });
Run Code Online (Sandbox Code Playgroud)
不起作用.怎么能正确完成?
我试图从我的渲染视图重构以下代码:
<Button href="#" active={!this.state.singleJourney} onClick={this.handleButtonChange.bind(this,false)} >Retour</Button>
Run Code Online (Sandbox Code Playgroud)
到绑定在构造函数中的版本.原因是渲染视图中的绑定会给我带来性能问题,特别是在低端手机上.
我创建了以下代码,但我不断收到以下错误(很多错误).看起来应用程序进入循环:
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
Run Code Online (Sandbox Code Playgroud)
以下是我使用的代码:
var React = require('react');
var ButtonGroup = require('react-bootstrap/lib/ButtonGroup');
var Button = require('react-bootstrap/lib/Button');
var Form = require('react-bootstrap/lib/Form');
var FormGroup = require('react-bootstrap/lib/FormGroup');
var Well = require('react-bootstrap/lib/Well');
export default class Search extends React.Component {
constructor() …Run Code Online (Sandbox Code Playgroud) 当react组件状态发生变化时,将调用render方法.因此,对于任何状态更改,可以在呈现方法主体中执行操作.那么setState回调是否有特定的用例?
我是React JS的新手(就像今天刚刚开始的那样).我不太明白setState是如何工作的.我将React和Easel JS结合起来根据用户输入绘制网格.这是我的JS bin:http://jsbin.com/zatula/edit? js, output
这是代码:
var stage;
var Grid = React.createClass({
getInitialState: function() {
return {
rows: 10,
cols: 10
}
},
componentDidMount: function () {
this.drawGrid();
},
drawGrid: function() {
stage = new createjs.Stage("canvas");
var rectangles = [];
var rectangle;
//Rows
for (var x = 0; x < this.state.rows; x++)
{
// Columns
for (var y = 0; y < this.state.cols; y++)
{
var color = "Green";
rectangle = new createjs.Shape();
rectangle.graphics.beginFill(color);
rectangle.graphics.drawRect(0, 0, …Run Code Online (Sandbox Code Playgroud) 我知道React可以异步和批量执行状态更新以进行性能优化.因此,在调用之后,您永远不能相信要更新的状态setState.但是你可以信任的反应更新相同的顺序状态setState被称为对
考虑单击以下示例中的按钮:
1.是否有可能a是假的,b对于:
class Container extends React.Component {
constructor(props) {
super(props);
this.state = { a: false, b: false };
}
render() {
return <Button onClick={this.handleClick}/>
}
handleClick = () => {
this.setState({ a: true });
this.setState({ b: true });
}
}
Run Code Online (Sandbox Code Playgroud)
2.是否有可能a是假的,b对于:
class SuperContainer extends React.Component {
constructor(props) {
super(props);
this.state = { a: false };
}
render() {
return <Container setParentState={this.setState.bind(this)}/>
}
}
class Container …Run Code Online (Sandbox Code Playgroud) 所以我有这个:
let total = newDealersDeckTotal.reduce(function(a, b) {
return a + b;
},
0);
console.log(total, 'tittal'); //outputs correct total
setTimeout(() => {
this.setState({dealersOverallTotal: total});
}, 10);
console.log(this.state.dealersOverallTotal, 'dealersOverallTotal1'); //outputs incorrect total
Run Code Online (Sandbox Code Playgroud)
newDealersDeckTotal只是一个数字数组,[1, 5, 9]但是this.state.dealersOverallTotal不能给出正确的总数但是total呢?我甚至提出了超时延迟,看看是否解决了这个问题.任何明显的或我应该发布更多的代码?
我正在研究todo应用程序.这是违规代码的一个非常简化的版本.我有一个复选框:
<p><input type="checkbox" name="area" checked={this.state.Pencil} onChange={this.checkPencil}/> Writing Item </p>
Run Code Online (Sandbox Code Playgroud)
这是调用复选框的函数:
checkPencil(){
this.setState({
pencil:!this.state.pencil,
});
this.props.updateItem(this.state);
}
Run Code Online (Sandbox Code Playgroud)
updateItem是一个映射到dispatch to redux的函数
function mapDispatchToProps(dispatch){
return bindActionCreators({ updateItem}, dispatch);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我调用updateItem action和console.log状态时,它总是落后一步.如果取消选中该复选框而不是true,我仍然会将true状态传递给updateItem函数.我是否需要调用另一个函数来强制状态更新?
我正在React中编写一个应用程序,无法避免出现一个超级常见的陷阱,该陷阱正在调用setState(...)after componentWillUnmount(...)。
我非常仔细地查看了我的代码,并尝试放置一些保护子句,但是问题仍然存在,并且我仍在观察警告。
因此,我有两个问题:
Warning: Can't perform a React state update on an unmounted component.
This is a no-op, but it indicates a memory leak in your application.
To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount
method.
in TextLayerInternal (created by Context.Consumer)
in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep …Run Code Online (Sandbox Code Playgroud) class MyHome extends StatefulWidget {
@override
State<StatefulWidget> createState() => new MyHomePage2();
}
class MyHomePage2 extends State<MyHome> {
List items = new List();
buildlist(String s) {
setState(() {
print("entered buildlist" + s);
List refresh = new List();
if (s == 'button0') {
refresh = [
new Refreshments("Watermelon", 250),
new Refreshments("Orange", 275),
new Refreshments("Pine", 300),
new Refreshments("Papaya", 225),
new Refreshments("Apple", 250),
];
} else if (s == 'button1') {
refresh = [
new Refreshments("Pina Colada", 250),
new Refreshments("Bloody Mary", 275),
new Refreshments("Long …Run Code Online (Sandbox Code Playgroud) 提前致谢。我有一个如下的状态数组。
我需要向状态数组添加一个项目,我发现我们不需要进行状态突变。我如何使用 prevState 设置状态。
const [messages, setMessages] = React.useState(
[
{
_id: 12,
createdAt: new Date(),
text: 'All good',
user: {
_id: 1,
name: 'Sian Pol',
}
},
{
_id: 21,
createdAt: "2019-11-10 22:21",
text: 'Hello user',
user: {
_id: 2,
name: 'User New'
}
}]
)
Run Code Online (Sandbox Code Playgroud)
我如何调用 set State 来附加这个状态数组。
像这样的东西?
setMessages(previousState => ({...stat
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我获得上述行代码。
setstate ×10
reactjs ×9
javascript ×5
state ×3
callback ×1
constructor ×1
dart ×1
ecmascript-6 ×1
flutter ×1
listview ×1
lodash ×1
react-hooks ×1
react-native ×1
redux ×1
size ×1
typescript ×1