英文:
How to properly re-write AngularJS code to React?
问题
以下是您提供的代码的中文翻译部分:
AngularJS(部分代码):
<script>
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
// 在初始化时调用,如果本地存储中存在数组则读取
$scope.checkAndInitiateFromLocalStorage = function() {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
$scope.arr = JSON.parse(readArrayStr);
} else {
$scope.arr = [{
name: "user1"
},
{
name: "user2"
},
{
name: "user3"
},
{
name: "user4"
}
];
}
}
// 在每次更新时调用,将最新状态存储在本地存储中
$scope.updateLocalStorage = function() {
localStorage.setItem('messagingArray', JSON.stringify($scope.arr));
/* console.log("更新本地存储!!"); */
}
$scope.checkAndInitiateFromLocalStorage();
});
</script>
React(部分代码):
<script src="react.min.js"></script>
<script src="react-dom.min.js"></script>
<script src="browser.min.js"></script>
<div id="app"></div>
<script type="text/babel">
class Application extends React.Component {
constructor(props) {
super(props);
// 在初始化时调用,如果本地存储中存在数组则读取
const checkAndInitiateFromLocalStorage = () => {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
this.state.arr = JSON.parse(readArrayStr);
} else {
this.state = {
arr: [
{ name: "user1" },
{ name: "user2" },
{ name: "user3" },
{ name: "user4" }
]
};
}
}
this.checkAndInitiateFromLocalStorage = this.checkAndInitiateFromLocalStorage.bind(this);
this.updateLocalStorage = this.updateLocalStorage.bind(this);
}
// 在每次更新时调用,将最新状态存储在本地存储中
updateLocalStorage() {
localStorage.setItem('messagingArray', JSON.stringify(this.state.arr));
};
componentDidMount() {
this.checkAndInitiateFromLocalStorage();
this.updateLocalStorage();
}
render() {
return (
<div>
<h3>显示数据</h3>
<ul>
{this.state.arr.map((obj, i) => (
<li key={i}>
{obj.name} - {obj.name}
</li>
))}
</ul>
</div>
);
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
</script>
这是您提供的AngularJS和React代码的翻译。如有其他问题或需要进一步的翻译,请告诉我。
英文:
I have the code below which reads stores and updates data to and from from localstorage and it works fine with AngularJS.
Here is my issue:
Am trying to re-write the Angularjs code to React but it throws error unexpected token.
AngularJS:
<script>
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
//called at initiazation, reads from localstorage if array is present there
$scope.checkAndInitiateFromLocalStorage = function() {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
$scope.arr = JSON.parse(readArrayStr);
} else {
$scope.arr = [{
name: "user1"
},
{
name: "user2"
},
{
name: "user3"
},
{
name: "user4"
}
];
}
}
//called at each update, stores the latest status in localstorage
$scope.updateLocalStorage = function() {
localStorage.setItem('messagingArray', JSON.stringify($scope.arr));
/* console.log("updated local storage !!"); */
}
$scope.checkAndInitiateFromLocalStorage();
});
</script>
React:
<script src="react.min.js"></script>
<script src="react-dom.min.js"></script>
<script src="browser.min.js"></script>
<div id="app"></div>
<script type="text/babel">
class Application extends React.Component {
constructor(props) {
super(props);
//called at initiazation, reads from localstorage if array is present there
//const checkAndInitiateFromLocalStorage () {
const checkAndInitiateFromLocalStorage = () => {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
this.state.arr = JSON.parse(readArrayStr);
} else {
this.state = {
arr: [
{ name: "user1"},
{ name: "user2"},
{ name: "user3"},
{ name: "user4"}
],
};
}
}
this.checkAndInitiateFromLocalStorage = this.checkAndInitiateFromLocalStorage.bind(this);
this.updateLocalStorage = this.updateLocalStorage.bind(this);
}
//called at each update, stores the latest status in localstorage
updateLocalStorage() {
localStorage.setItem('messagingArray', JSON.stringify(this.state.arr));
};
componentDidMount() {
this.checkAndInitiateFromLocalStorage();
this.updateLocalStorage();
}
render() {
return (
<div>
<h3>Display Data</h3>
<ul>
{this.state.arr.map((obj, i) => (
<li key={i}>
{obj.name} - {obj.name}
</li>
))}
</ul>
</div>
);
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
</script>
Here is the updated react error message:
Uncaught TypeError: Cannot read properties of undefined (reading 'bind')
at new Application (eval at transform.run
答案1
得分: 0
这个问题通过将checkAndInitiateFromLocalStorage的定义移到构造函数之外来解决。
以下是代码
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
arr: [
{ name: "user1" },
{ name: "user2" },
{ name: "user3" },
{ name: "user4" }
]
};
this.checkAndInitiateFromLocalStorage = this.checkAndInitiateFromLocalStorage.bind(this);
this.updateLocalStorage = this.updateLocalStorage.bind(this);
}
checkAndInitiateFromLocalStorage() {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
this.state.arr = JSON.parse(readArrayStr);
} else {
this.state.arr;
}
}
updateLocalStorage() {
localStorage.setItem('messagingArray', JSON.stringify(this.state.arr));
}
componentDidMount() {
this.checkAndInitiateFromLocalStorage();
this.updateLocalStorage();
}
render() {
return (
<div>
<h3>Display Data</h3>
<ul>
{this.state.arr.map((obj, i) => (
<li key={i}>
{obj.name} - {obj.name}
</li>
))}
</ul>
</div>
);
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
请注意,我已经将HTML标记的编码(如"
和<
)还原为原始代码,以便更容易阅读。
英文:
This issue was solved by Moving the definition of checkAndInitiateFromLocalStorage outside of the constructor.
Here is the code below
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
arr: [
{ name: "user1"},
{ name: "user2"},
{ name: "user3"},
{ name: "user4"}
],
};
this.checkAndInitiateFromLocalStorage = this.checkAndInitiateFromLocalStorage.bind(this);
this.updateLocalStorage = this.updateLocalStorage.bind(this);
}
checkAndInitiateFromLocalStorage () {
//const checkAndInitiateFromLocalStorage = () => {
var readArrayStr = localStorage.getItem('messagingArray');
if (readArrayStr && readArrayStr.length > 0) {
this.state.arr = JSON.parse(readArrayStr);
} else {
this.state.arr;
}
}
//called at each update, stores the latest status in localstorage
updateLocalStorage() {
localStorage.setItem('messagingArray', JSON.stringify(this.state.arr));
};
componentDidMount() {
this.checkAndInitiateFromLocalStorage();
this.updateLocalStorage();
}
render() {
return (
<div>
<h3>Display Data</h3>
<ul>
{this.state.arr.map((obj, i) => (
<li key={i}>
{obj.name} - {obj.name}
</li>
))}
</ul>
</div>
);
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论