我的项目是Cordova + React(es6)。 我在使用setState()时遇到了一些问题。
传递给state的数据不为空。 但是,将数据传递给状态,我收到结果状态为空。
我的项目的env(package.json):
#include <string>
#include <iostream>
using namespace std;
int main() {
string str;
long szam = 0, Dec, num, Base = 1, x,i=1;
cout << "the number : ";
cin >> str;
cout << "The input number system:";
cin >> x;
while (str[i] == '\0')
{
if (str[i] = 'A') { num = 10;}
else if (str[i] = 'B') { num = 11; }
else if (str[i] = 'C') { num = 12; }
else if (str[i] = 'D') { num = 13; }
else if (str[i] = 'E') { num = 14; }
else if (str[i] = 'F') { num = 15; }
else { num = str[i] - '0'; }
Dec = Dec + num * Base;
Base = Base * x;
}
cout << szam << endl;
return 0;
我的代码:
{
"name": "MYAPP",
"version": "1.0.0",
"description": "this is front of simula's app",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "browserify ./www/jsx/app.jsx -t babelify -o ./www/js/app.js"
},
"author": "maki",
"license": "ISC",
"dependencies": {
"babel-preset-es2015": "^6.13.2",
"browserify": "^13.1.0",
"gulp": "^3.9.1",
"history": "^4.2.0",
"material-design-icons": "^2.2.3",
"material-ui": "^0.15.4",
"rd3": "^0.7.2",
"react": "^15.3.0",
"react-d3": "^0.4.0",
"react-dom": "^15.3.1",
"react-motion": "^0.4.4",
"react-router": "^2.7.0",
"react-swipeable-views": "^0.7.0",
"react-tap-event-plugin": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-1": "^6.13.0",
"babelify": "^7.3.0",
"gulp-sass": "^2.3.2",
"jquery": "^3.1.0"
}
}
我该怎么做???
答案 0 :(得分:0)
React setState()
是一个异步操作,因此在您的代码中调用后不会立即传递结果。如果在您的render()方法中添加console.log(this.state.data_source);
,则可以访问更新后的状态,该方法仅在this.setState()
完成时调用。
请使用React文档:https://facebook.github.io/react/docs/component-api.html#setstate
setState()不会立即改变this.state,但会创建挂起状态转换。调用此方法后访问this.state可能会返回现有值。 无法保证对setState的调用同步操作,并且可以对调用进行批处理以获得性能提升。
setState()将始终触发重新渲染,除非在shouldComponentUpdate()中实现条件渲染逻辑。
答案 1 :(得分:0)
除了Piotr'a答案之外,setState
可以传递一个用新状态调用的回调,例如;
this.setState({data_source: final_cash_data}, function(newState){
console.log(newState.data_source)
});