简体   繁体   English

React 如何将数据从组件传递到另一个组件

[英]how to pass data from component to another component in React

How to pass data between React components, for example I have two component the first one called 'ButtonComp' and the second is 'InputComp', ButtonComp have one button that change the background color and the InputComp have one input filed now I want to take the string form the InputCome component and send it to ButtonComp, so when the user type 'green' in the InputComp and click the button its change the color, what is the approach for this.如何在 React 组件之间传递数据,例如我有两个组件,第一个称为“ButtonComp”,第二个是“InputComp”,ButtonComp 有一个更改背景颜色的按钮,InputComp 有一个输入文件,现在我想采取字符串形成 InputCome 组件并将其发送到 ButtonComp,因此当用户在 InputComp 中键入“绿色”并单击按钮时,它会更改颜色,这是什么方法。

similar to benjamins, slight different in passing props:)类似于benjamins,传递道具略有不同:)

 const {useState} = React; const App = () => { const [bg, setBg] = useState(""); const [color, setColor] = useState(""); return ( <div style={{background: bg, height: "100vh"}}> <InputComp color={color} setColor={setColor}/> <br/> <br/> <ButtonComp color={color} setBg={setBg}/> </div> ); }; const ButtonComp = ({color, setBg}) => { return(<button onClick={()=> setBg(color)}>{color? `Click to change BG to ${color}`: "Please Input & Click to change BG"}</button>) } const InputComp = ({setColor, color}) => { return(<input type="text" value={color} onChange={(e)=>setColor(e.target.value)} />) } // Render it ReactDOM.createRoot( document.getElementById("root") ).render( <App/> );
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>

In order to share "state" between components, you need to "lift state up".为了在组件之间共享“状态”,您需要“向上提升 state”。 This means creating state in a component that is rendered higher in the component tree, than the components that need to share that state.这意味着在组件树中呈现更高的组件中创建 state,而不是需要共享该 state 的组件。

In the example below.在下面的例子中。 State is held in the App component, and is passed down to the InputComp component, and shared into the click handler for ButtonComp component. State 保存在 App 组件中,并向下传递给 InputComp 组件,并共享到 ButtonComp 组件的单击处理程序中。

When ButtonComp is clicked, the input state is copied to the background state, which is then rendered into the css on the div component.点击 ButtonComp 时,输入 state 被复制到后台 state,然后渲染到 div 组件上的 css 中。

This is a very basic example, but demonstrates how to accomplish your goal.这是一个非常基本的示例,但演示了如何实现您的目标。

 const InputComp = (props) => { return <input {...props} />; }; const ButtonComp = (props) => { return <button {...props} />; }; const App = (props) => { const [background, setBackground] = React.useState('') const [input, setInput] = React.useState('') return ( <div style={{ background: background }}> <div> <InputComp value={input} onChange={(e) => setInput(e.target.value)} /> </div> <div> <ButtonComp onClick={() => setBackground(input)}>Submit</ButtonComp> </div> </div> ); }; ReactDOM.createRoot(document.getElementById('root')).render(<App />);
 <div id="root" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM