0

这是我第一次制作有限状态自动机。我尝试制作一个停止灯类型的程序,如果您单击按钮,灯会改变一次,从绿色开始,如果再次单击则变为黄色,然后变为红色,然后再次循环。我设法使它工作,除了一个小错误。在屏幕更新之前需要单击它两次,我真的不知道如何修复它。

我在控制台上注意到,当我单击它时 currentLightState 会发生变化,但在第一次单击时会恢复为以前的颜色。我发现这是因为它与我的状态不同步。但是,当我尝试将 currentLightState 放入类中并分配 this.state.light 时,currentLightState 变得未定义。我尝试在 this.state.light 的末尾添加一个 .bind(this),但我只是收到另一个错误,说它不是一个函数。有什么建议么?

我没有发布我的更新函数或我的 onHandleClick 函数,因为它不直接处理 currentLightState。

const lightMachine = {
  green:{
    LIGHT: 'yellow'
  },
  yellow:{
    LIGHT: 'red'
  },
  red:{
    LIGHT: 'green'
  }
}

// current location of currentLightState
let currentLightState = 'green';
class App extends React.Component{
    constructor(props){
        super(props);
        this.state = {
          light: 'green'    //initial value
        };
      }
    transition(state, action){
      // This is where my currentLightState messes up the first run 
      currentLightState = this.state.light

      const nextLightState =  lightMachine[currentLightState][action]
      this.setState({light: nextLightState}) 
    }
    render(){

        return(
            <div>
              <button onClick={this.onHandleClick.bind(this)}>
                change the Light!
              </button>
                {currentLightState}
            </div>
            );
        }
    }

编辑:这是我的 onHandleClick 功能:)

    onHandleClick(){
      this.update(currentLightState)

    };

另外,我想我只需用 this.state.light 替换渲染函数中的 currentLightState 就解决了我的问题。

如果这是否是一个合法的修复,我想知道,但它目前似乎有效。

如果有人仍然可以回答为什么当您将 currentLightState 放入类中并将 state.light 分配给它时,它会变得未定义,那就太好了。这将有助于扩展我的 React 知识 :)

4

1 回答 1

0

欢迎来到 Jr194 板!

就主题而言,我认为保持currentLightlightMachine作为由组件状态管理的值将是有益的。这有助于确保您的所有逻辑都在同一个上下文中,避免诸如“blah blah is not defined”之类的奇怪错误。

其次,您似乎已经定义了一些真正可以缩小到一个的功能。所有人似乎都在尝试做同样的事情update, onHandleClicktransition

最后,考虑重新组织您的代码,lightMachine使其更加直观,以便您可以快速导航到下一个亮点。您已经知道父键中的当前颜色是什么,他们真的需要让它的对象包含另一个具有相同值的键吗?

考虑以下代码:

class App extends React.Component {
  state = {
    currentLight: "green",
    lightMachine: {
      green: {
        nextLight: "yellow"
      },
      yellow: {
        nextLight: "red"
      },
      red: {
        nextLight: "green"
      }
    }
  };

  transition = () => {
    const currentLight = this.state.currentLight;
    const nextLight = this.state.lightColors[currentLight].nextLight;
    this.setState({
      currentLight: nextLight
    });
  };
  render() {
    return (
      <div>
        <button onClick={this.transition}>Click Me</button>
        <div>{this.state.currentLight}</div>
      </div>
    );
  }
}

这应该可以让您按预期快速更改灯光。这是沙箱:https ://codesandbox.io/s/4x08ovyjp7

如果您有任何问题,请告诉我:)

另外关于你的问题,当你把它放在课堂上时,为什么currentLightState会给你一个错误。render这不是 React 问题,而是 JavaScript 问题。

让我们看一些例子:

const test= "hello"
class Example extends React.Component{
   state = {
       greeting: "woof"
   }
   render(){
     <div>{test}</div>
   }
}

如您所知,这不会给我们任何错误。我们正在利用在我们的类之外定义的变量,这完全没问题。

现在让我们看看这个

class Example extends React.Component{
   state = {
       greeting: "woof"
   }

   test = this.state.greeting

   render(){
     <div>{test}</div>
   }
}

这给了我们一个错误。为什么,因为在您的渲染方法中,您将测试视为一个变量,而实际上它不是。在class您编写的对象中,currentLightState现在test被视为properties,而不是变量。为了访问类中的属性,您需要使用“ this”关键字,您已经使用过的关键字this.update, this.handleOnClick等等,它们也是属性。

现在我们知道这段代码可以工作。

class Example extends React.Component{
   state = {
       greeting: "woof"
   }

   test = this.state.greeting

   render(){
     <div>{this.test}</div>
   }
}
于 2019-05-01T03:24:02.770 回答