2

I am learning how to use react-ketting

There are some examples of using it on the wiki

And in UseCollection section I am stuck

Could someone explain what is this and how it works:

function MyCollectionItem({resource}: { resource: Resource<Article> }) {

Full snippet is:

function MyCollection() {

  const {
    loading,
    error,
    items
  } = useCollection<Article>('/articles');

  if (loading) return <div>loading...</div>;
  if (error) return <div class="error">boo</div>;

  return <ul>{items.map(item => <MyCollectionItem resource={item} />)}</ul>

}

function MyCollectionItem({resource}: { resource: Resource<Article> }) {
  const {
    loading,
    error,
    data
  } = useResource(resource);

  if (loading) return <div>loading...</div>;
  if (error) return <div className="error">boo</div>;

  return <li>{data.title} - {data.body}</li>;
}

I want to add string prop to MyCollectionItem like

function MyCollectionItem({resource}: { resource: Resource<Article> }, anotherOption: string) {

And call it

<MyCollectionItem·resource={item}·anotherOption='project'·key={item.uri}·/>

Got error

[tsserver 2322] [E] Type '{ resource: Resource<Article>; anotherOption: string; key: string; }' is not assignable to type 'IntrinsicAttributes & { resource: Resource<Article>; }'. Property 'anotherOption' does not exist on type 
 IntrinsicAttributes & {  resource: Resource<Article>; }'

1 Answer 1

1

By React covention, all component props are passed as an object as the 1st argument of your Functional Component.

So if you call the component:

<MyCollectionItem
  resource={item}
  anotherOption='project'
/>

React will actually call the function like:

MyCollectionItem({
  resource: item,
  anotherOption: "project"
})

Therefore your extra prop should be an extra member of the 1st argument (usually through destructuring), instead of a 2nd positional argument:

function MyCollectionItem({
  resource,
  anotherOption
}: {
  resource: Resource<Article>;
  anotherOption: string;
}) {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.