Javascript React中是否有更新DOM的特殊方法?
您好,我正在React中制作一个flashcard应用程序,我正在尝试在两张卡之间切换,但在我切换卡之前,它们不会更新 我尝试将索引currentCard设置为state而不是全局,但在实现时遇到了一些问题。我也尝试过这个.forceUpdate,但结果都是未定义的Javascript React中是否有更新DOM的特殊方法?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,您好,我正在React中制作一个flashcard应用程序,我正在尝试在两张卡之间切换,但在我切换卡之前,它们不会更新 我尝试将索引currentCard设置为state而不是全局,但在实现时遇到了一些问题。我也尝试过这个.forceUpdate,但结果都是未定义的 import React from 'react'; import './App.css'; import 'bootstrap/dist/css/bootstrap.min.css'; // variables let curr
import React from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
// variables
let currentCard = 0;
这是我获得卡片的地方:
const mockData = [
{
front: "Hello",
back: "Dobrý den"
},
{
front: "How are you?",
back: "Jak se máš?"
},
{
front: "I'm fine",
back: "Mám se dobře"
}
];
class LeftArrow extends React.Component {
constructor(props){
super(props);
this.state = {isClicked: false};
}
render(){
//checks it so it can't be less than zero
return(
<button
className="btn btn-primary"
onClick={() => this.props.onCardChange('LEFT')}
><</button>
)
}
}
class RightArrow extends React.Component {
constructor(props){
super(props);
this.state = {isClicked: false};
}
render(){
// checks it so it isn't more than the length of array
// checks if card has
return(
<button
className="btn btn-primary"
onClick={() => this.props.onCardChange('RIGHT')}
>
>
</button>
)
}
}
class Card extends React.Component {
constructor(props){
super(props);
// helps toggle between the front face and the back
this.state = {
isFront: true,
};
}
您必须将mockData和currentCard置于卡组件的状态,handleCardChange应使用此.setState{}更新状态 使用设置状态
this.setState({someState: "someNewState"})
大家好,欢迎来到Stack Overflow!你能把你的帖子缩小一点,只包含与你的问题相关的代码吗?对于许多人来说,这可能有点难以承受…setState使用setState组件方法更新组件的状态数据。以这种方式更新组件的状态将重新呈现该组件的DOM。
const mockData = [
{
front: "Hello",
back: "Dobrý den"
},
{
front: "How are you?",
back: "Jak se máš?"
},
{
front: "I'm fine",
back: "Mám se dobře"
}
];
class Card extends React.Component {
constructor(props) {
super(props);
// helps toggle between the front face and the back
this.state = {
isFront: true
currentCard: 0,
mockData,
};
}
this.setState({someState: "someNewState"})