اذهب إلى المحتوى
  • 0

تمرير prop الى props.children في react

Rayden Storm

السؤال

كيف يمكن تمرير handleSomething الى Post بدون أن أنقل الدالة handleSomething من المكون List 

import React from 'react';

 function List(props) {
const handleSomething = () => {
    ...
}

    return (
        <div>
            <h1>List</h1>
            {props.children}
        </div>
    )
}


 function Post(props) {
    return (
        <div>
            <button onClick={props.handleSomething}>send</button>
        </div>
    )
}


export default function Home() {
  return (
    <div>
      <List>
        <Post></Post>
      </List>
    </div>
  );
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

children نفوم باستخدام نمط الدالة في استخدام ال

import React from "react";

function List(props) {
  const handleSomething = () => {
    console.log("triggered");
  };

  return (
    <div>
      <h1>List</h1>
      {/* نقوم بتمرير الدالة للأطفال عن طريق استخدام الدالة وليس الأطفال مباشرة */}
      {props.children({ handleSomething })}
    </div>
  );
}

function Post(props) {
  return (
    <div>
      {/* نقوم باستخام الدالة هنا  */}
      <button onClick={props.handleSomething}>send</button>
    </div>
  );
}

export default function Home() {
  return (
    <div>
      <List>
        {/* children نفوم باستخدام نمط الدالة في استخدام ال  */}
        {/*List ونقوم بنشر الخصائص التي تم إرسالها من المكون  */}
        {props => <Post {...props} />}
      </List>
    </div>
  );
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...