ListItemからプレビューコンポーネントにアイテムの小道具にアクセスしてレンダリングしようとしています。アイテムの小道具をLink to Previewコンポーネント経由で渡すにはどうすればいいですか?
const RecipeListItem = (props) => {
return (
<Card>
<Link to={`/recipes/${props.recipe.id}`}>
<Image src={props.recipe.url} />
</Link>
<Card.Content>
<Card.Header>
{props.recipe.title}
</Card.Header>
<Card.Description>
{props.recipe.description}
</Card.Description>
</Card.Content>
</Card>
)
}
export default RecipeListItem;
私はリンクをクリックして、
私はプレビューコンポーネントを参照してくださいhttp://localhost:3000/recipes/1
にリダイレクトが、IDを持つアイテムにアクセスすることはできません:1つの小道具は、ページ上にレンダリングします。ここで
は私のプレビューコンポーネントです:
const RecipePreview = (props) => {
return (
<Card>
<Image src={props.match.params.url} />
<Card.Content>
<Card.Header>
{props.match.params.title}
</Card.Header>
<Card.Description>
{props.match.params.description}
</Card.Description>
</Card.Content>
</Card>
)
}
export default RecipePreview;
ルート:私はこのようなルックスを反復処理しています
const Main =() => (
<main>
<Switch>
<Route exact path='/recipes' component={RecipeList}/>
<Route path='/create' component={AddRecipe}/>
<Route path='/recipes/:recipeId' component={RecipePreview}/>
</Switch>
</main>
)
export default Main;
私のサンプルデータ:
constructor(props) {
super(props);
this.state = {
recipes: {
"1": {
id: 1,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"2": {
id: 2,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
},
"3": {
id: 3,
url: 'http://via.placeholder.com/300x300',
title: '1st Title',
description: 'some decription 1'
}
}
}
}
あなたはプレビューコンポーネントのコードをしてください投稿することができますか? – palsrealm
私の投稿をPreviewコンポーネントで更新しました..エラー 'props'が定義されていません ' – karolis2017
あなたは' recipeId'を正しく読んでいますか?これは 'props.params.recipeId'にあります。 –