简体   繁体   English

如何从另一个文件中导出钩子局部变量以及反应中的组件?

[英]how to export a hook local variable from another file along with the component in react?

i want to use the Aya varriable in app.js (the code below is in a component) but i can't export it as it's local to the function我想在 app.js 中使用 Aya 变量(下面的代码在一个组件中)但我无法导出它,因为它是函数的本地变量

function BasicExample() {
    const [Aya, setAya] = useState({data});
   // code containing modifications to apply to Aya
}
export default BasicExample

Lift state up . 状态提升 Basically define the state in App and pass a handler that does the update to the state down to your child component so it can call it with a new value.基本上在 App 中定义状态,并将对状态进行更新的处理程序传递给您的子组件,以便它可以使用新值调用它。

In this example I have the main component which renders a child component.在此示例中,我有呈现子组件的主要组件。 In that component is a div that displays the count from the parent state, and a button to increment its value.在该组件中有一个显示父状态count的 div 和一个用于递增其值的按钮。 Note that a function (the handler) is written in the parent component, and then a reference to it is passed down into the props of the child component - along with the value.请注意,一个函数(处理程序)写在父组件中,然后对它的引用连同值一起传递到子组件的 props 中。

When the button is clicked the function is called.当按钮被点击时,函数被调用。 This updates the state which causes a new render, and the updated state is reflected in the child component by the updated value.这会更新导致新渲染的状态,并且更新后的状态会通过更新后的值反映在子组件中。

 const { Fragment, useState } = React; function Example() { // Initialise state const [ count, setCount ] = useState(0); // A function called `handleClick` that // updates the state. This handler is passed down // to the child component via its props function handleClick() { setCount(prev => prev + 1); } // The child component accepts both the // `count` state, and the handler as its props return ( <ChildComponent count={count} handleClick={handleClick} /> ); } // We destructure the count and the (reference to the) // function from the component's props. // When the button is clicked it calls the handler. // Changes to the state are reflected here when the parent // component re-renders function ChildComponent({ count, handleClick }) { return ( <Fragment> <div>{count}</div> <button type="button" onClick={handleClick} >Increment count </button> </Fragment> ); } ReactDOM.render( <Example />, document.getElementById('react') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量导出到另一个反应功能组件? - How to export a variable to another react functional component? React hook:如何在反应中从另一个组件调用模态组件 - React hook: How to call Modal component from another component in react 如何从组件中获取变量并在另一个组件或文件中使用它 React - How to take variable from component and use it in another component or file React 如何从另一个js文件访问react组件中定义的变量? - How to access a variable defined in a react component from another js file? React.js 如何从另一个没有导出的 js 文件中传递变量 - React.js How to pass a variable from another js file that does not have export 有没有办法从 React 组件中导出变量? - Is there any way to export a variable from an React component? 在反应 function 中使用 use-State 钩子总是抛出错误 + 如何与另一个组件共享变量 - Using use-State hook in react function always throws an error + How to share a variable with another component 如何在React中将状态(用户选择的值)从一个组件导出到另一个组件? - How to export a state(user selected value) from one component to another component in React? 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 如何在反应原生组件中显示来自本地存储的文件预览? - How to display a preview of file from local storage in a react native component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM