<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amir fattahpasand</title>
    <description>The latest articles on DEV Community by Amir fattahpasand (@amirsaver).</description>
    <link>https://dev.to/amirsaver</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1318961%2F53b6dc37-e7d7-4b9d-a87b-faee9a5a1650.jpeg</url>
      <title>DEV Community: Amir fattahpasand</title>
      <link>https://dev.to/amirsaver</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amirsaver"/>
    <language>en</language>
    <item>
      <title>Slate js editor is always the last one (React)</title>
      <dc:creator>Amir fattahpasand</dc:creator>
      <pubDate>Sat, 02 Mar 2024 02:51:35 +0000</pubDate>
      <link>https://dev.to/amirsaver/slate-js-editor-is-always-the-last-one-react-5f5j</link>
      <guid>https://dev.to/amirsaver/slate-js-editor-is-always-the-last-one-react-5f5j</guid>
      <description>&lt;p&gt;Hi, Can anyOne help!? my problem is that slate editor is always the last one and I can't add emoji to other editors (1st, 2nd) &lt;br&gt;
⁠❓javascript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import img from './images/julinAva.jpg'
import UserBdInput from './userBdInput'


const todayBirthdays = [
    {name: 'Amir', id: 'amirId', gender: 'male' , img: img},
    {name: 'fatima', id: 'fatimaId', gender: 'female', img: img},
    {name: 'Reza', id: 'RezaId', gender: 'male', img: img},
]


function Birthdays() {

  return (
     &amp;lt;div className='w-[700px] flex flex-col bg-white mx-auto p-3 rounded-[7px]'&amp;gt;
      &amp;lt;h2 className='text-xl font-bold mb-3'&amp;gt;Today Birthdays&amp;lt;/h2&amp;gt;

        {todayBirthdays.map((f , i)=&amp;gt;(
         &amp;lt;main key={f.id}&amp;gt;
          &amp;lt;div  className='flex flex-row w-full gap-2'&amp;gt;
              &amp;lt;img src={f.img} alt="" className='w-[60px] h-[60px] rounded-full'/&amp;gt;
              &amp;lt;span className='flex flex-col w-full relative'&amp;gt;
                &amp;lt;h3&amp;gt;{f.name}&amp;lt;/h3&amp;gt;
                &amp;lt;UserBdInput userId={f.id} userGender={f.gender}/&amp;gt;
              &amp;lt;/span&amp;gt;
          &amp;lt;/div&amp;gt;
              {(todayBirthdays.length - 1 &amp;gt; i) &amp;amp;&amp;amp; &amp;lt;hr className='w-[95%] mx-auto my-6 h-[2px] bg-gray-400' /&amp;gt;}
         &amp;lt;/main&amp;gt;

        ))}

    &amp;lt;/div&amp;gt;
  )
}

export default Birthdays
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { Transforms, createEditor } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
import { BaseEditor, Descendant } from 'slate'
import { ReactEditor } from 'slate-react'
import EmojiBD from './emojiBD'
import { useCallback, useState } from 'react'


type CustomElement = { type: 'paragraph'; children: CustomText[] }
type CustomText = { text: string }

declare module 'slate' {
  interface CustomTypes {
    Editor: BaseEditor &amp;amp; ReactEditor
    Element: CustomElement
    Text: CustomText
  }
}
const initialValue: Descendant[]= [
    {
      type: 'paragraph',
      children: [{ text: '' }]
    }
  ]



function UserBdInput({userId, userGender}) {
       const [editor] = useState(() =&amp;gt; withReact(createEditor()))

        function insertText(editor,text: string) {
            if (!text) return;
            Transforms.insertText(editor, text); 
            console.log(editor)
        } 

        const renderElement = useCallback(props =&amp;gt; {
            switch (props.element.type) {
              case 'code':
                return &amp;lt;CodeElement {...props} /&amp;gt;
              default:
                return &amp;lt;DefaultElement {...props} /&amp;gt;
            }
          }, [])

          const CodeElement = props =&amp;gt; {
            return (
              &amp;lt;pre {...props.attributes}&amp;gt;
                &amp;lt;code&amp;gt;{props.children}&amp;lt;/code&amp;gt;
              &amp;lt;/pre&amp;gt;
            )
          }

          const DefaultElement = props =&amp;gt; {
            return &amp;lt;p {...props.attributes}&amp;gt;{props.children}&amp;lt;/p&amp;gt;
          }


  return (
        &amp;lt;Slate editor={editor} initialValue={initialValue}&amp;gt;
           &amp;lt;Editable renderElement={renderElement} id={userId} placeholder={`Write on ${userGender == 'male' ? 'his' : 'her'} Timeline...`} 
             className={`relative bg-fbBg text-black pr-7 focus:outline-none rounded-[20px] w-full
              p-2 font-semibold text-md text-gray-600`}&amp;gt;
           &amp;lt;/Editable&amp;gt;
           &amp;lt;span className={`emojiSpan absolute bottom-3 right-2 z-[10]`}&amp;gt;&amp;lt;EmojiBD onAddEmoji={(emj) =&amp;gt; { insertText(editor, emj) }} /&amp;gt;&amp;lt;/span&amp;gt;       

        &amp;lt;/Slate&amp;gt;
  )
}

export default UserBdInput
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think the issue is from useState that does not create unique editors for each  &amp;lt;Editable&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
