D
D
Dmitry Markov2022-03-15 15:50:26
React
Dmitry Markov, 2022-03-15 15:50:26

How to pass to props?

Good afternoon.
Tell me how can I pass some props to a component that is called like this:

const mapper = {
  '{user}': <UserAddon />,
  '{post}': <PostAddon />,
  '{group}': <GroupAddon />,
};
export const ContentRender = () => {
  const content = "{user} опубликовал(а) {post} в группе {group}";
 return (
      {content.split(/({.*?})/g).map((child) => (
        <>{child.startsWith("{") ? mapper[child.trim()] : child}</>
      ))}
  );
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2022-03-15
@En-Tilza

https://codesandbox.io/s/qna-q-1127408-6izxcy

G
GlazOtca, 2022-03-15
@GlazOtca

export const ContentRender = (props) => {
  const content = "{(props.user} опубликовал(а) {(props.post} в группе {(props.group}";
 return (
      {content.split(/({.*?})/g).map((child) => (
        <>{child.startsWith("{") ? mapper[child.trim()] : child}</>
      ))}
  );
}
// вызов компонента
<ContentRender user={mapper['{user}']} post={mapper['{post}']} group={mapper['{group}']}>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question