react组件传值的方法有哪些

其他教程   发布日期:2025年04月03日   浏览次数:142

这篇文章主要讲解了“react组件传值的方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“react组件传值的方法有哪些”吧!

1、父向子传值

父组件

  1. classAppextendsReact.Component{
  2. state={
  3. name:'jack',
  4. age:19,
  5. gender:'男',
  6. count:1
  7. }
  8. render() {
  9. return<div >
  10. 御剑乘风来,除魔天地间!
  11. <Child {...this.state} hobby={[1,2,3,4]}></Child>
  12. </div>
  13. }

子组件:

  1. const Child = (props) => {
  2. console.log(props,99);
  3. return<div>
  4. 御剑乘风来,除魔天地间!===Child======{props.count}
  5. </div>
  6. };

2、子向父传值

父组件

  1. classAppextendsReact.Component{
  2. state={
  3. name:'jack',
  4. age:19,
  5. gender:'男',
  6. count:1
  7. }
  8. handle2=(msg)=>{
  9. console.log(msg,77); // 123
  10. }
  11. render() {
  12. return<div >
  13. 御剑乘风来,除魔天地间!==
  14. <ChildgetMsg={this.handle2}></Child>
  15. </div>
  16. }

子组件:

  1. const Child = (props) => {
  2. console.log(props,99);
  3. var handle=()=>{
  4. console.log(111);
  5. props.getMsg('123')
  6. }
  7. return<div>
  8. 御剑乘风来,除魔天地间!===Child======
  9. <buttononClick={handle}>child1</button></div>
  10. };

3、非父子传值

child1 向 child2 传值

思路:child 传到app ,然后app传到child2

  1. classAppextendsReact.Component{
  2. state={
  3. name:'jack',
  4. age:19,
  5. gender:'男',
  6. count:1
  7. }
  8. handle2=(msg)=>{
  9. console.log(msg,77); // 123this.setState({
  10. count:this.state.count+msg
  11. })
  12. }
  13. render() {
  14. return<div >
  15. 御剑乘风来,除魔天地间!==
  16. <ChildgetMsg={this.handle2} ></Child>
  17. <Child2count={this.state.count}></Child2>
  18. </div>
  19. }

子组件1:

  1. const Child = (props) => {
  2. console.log(props,99);
  3. var handle=()=>{
  4. console.log(111);
  5. props.getMsg('123')
  6. }
  7. return<div>
  8. 御剑乘风来,除魔天地间!===Child======
  9. <buttononClick={handle}>child1</button></div>
  10. };
  11. const Child2 = (props) => {
  12. console.log(props,99);
  13. var handle=()=>{
  14. console.log(111);
  15. props.getMsg('123')
  16. }
  17. return<div>
  18. 御剑乘风来,除魔天地间!===Child======
  19. <buttononClick={handle}>child1</button></div>
  20. };

4、Context 方法 传值 【类似vue的 provide / inject】

步骤:

1- const {Provider,Consumer} = React.createContext()
2- Provider包裹父组件 定义value是需要传的值 ====<Provider value={this.state.count}>
3- Consumer包裹需要接收数据的组件 data接收数据

  1. <Consumer >
  2. {data=>(
  3. <div>
  4. 御剑乘风来,除魔天地间! ==Child2*****{data}
  5. </div>
  6. )}

代码如下:

  1. import React from 'react'const {Provider,Consumer} = React.createContext()
  2. const Child = (props) => {
  3. console.log(props,99);
  4. return <div>
  5. 御剑乘风来,除魔天地间!===Child
  6. </div>
  7. };
  8. classChild2extendsReact.Component{
  9. render() {
  10. return <div>
  11. <Consumer >
  12. {data=>(
  13. <div>
  14. 御剑乘风来,除魔天地间! ==Child2*****{data}
  15. </div>
  16. )}
  17. </Consumer>
  18. </div>
  19. }
  20. }
  21. classAppextendsReact.Component{
  22. state={
  23. name:'jack',
  24. age:19,
  25. gender:'男',
  26. count:1
  27. }
  28. render() {
  29. return <Provider value={this.state.count}>
  30. 御剑乘风来,除魔天地间!=={this.state.name}
  31. <Child></Child>
  32. <Child2></Child2>
  33. </Provider>
  34. }
  35. }
  36. export default App

以上就是react组件传值的方法有哪些的详细内容,更多关于react组件传值的方法有哪些的资料请关注九品源码其它相关文章!