Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在react中为外部变量提供状态?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在react中为外部变量提供状态?

Javascript 如何在react中为外部变量提供状态?,javascript,reactjs,Javascript,Reactjs,我想将我的react组件的外部变量绑定到此组件中的标记,这可能吗?该变量由我的组件触发的一些函数更改,我想给她状态,“以跟踪更改”,但问题是,该变量是外部的 我是react和javascript的新手,做了很多研究,现在我迷路了 我有一个像 function App() { ..... return ( <div className="App"> <div id="ias-registers"> <h3>PC: {PC}

我想将我的react组件的外部变量绑定到此组件中的标记,这可能吗?该变量由我的组件触发的一些函数更改,我想给她状态,“以跟踪更改”,但问题是,该变量是外部的

我是react和javascript的新手,做了很多研究,现在我迷路了

我有一个像

function App() {

.....

return (
    <div className="App">
      <div id="ias-registers">
        <h3>PC: {PC}</h3> )
....;
}

var PC;

也许这就是方法,但我做得不对使用redux,它为您提供了一个全局状态,就像浏览器中的本地存储一样,您可以从应用程序中的任何组件访问它

查找以下链接以进一步了解


为什么不使用redux来实现这一点,它就是为此目的而创建的。您需要使用redux中的
MapStateTrops
。请阅读

  const [values, setValues] = React.useState({
    multiline: '',
    IBR: IBR,
    PC: PC,
    IR: IR,
    MBR: MBR,
    MAR: MAR,
    AC: AC,
    MQ:MQ,
  });