Javascript 将道具传递给另一个文件中的组件redux
我需要使用redux将道具传递给位于另一个文件中的组件。将其作为道具传递给组件不起作用。任何帮助都将不胜感激,刚刚开始学习Redux 标题Javascript 将道具传递给另一个文件中的组件redux,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我需要使用redux将道具传递给位于另一个文件中的组件。将其作为道具传递给组件不起作用。任何帮助都将不胜感激,刚刚开始学习Redux 标题 导出类DisconnectedAllDogs扩展React.Component{ render(){ 返回( 彼得公园 {this.props.dogs.map(dog=>{ 返回 })} ) } } 导出常量MapStateTops=(状态)=>{ 返回{ 狗:州。狗 } } 导出常量mapDispatchToProps=(调度)=>{ 返回{ } }
导出类DisconnectedAllDogs扩展React.Component{
render(){
返回(
彼得公园
{this.props.dogs.map(dog=>{
返回
})}
)
}
}
导出常量MapStateTops=(状态)=>{
返回{
狗:州。狗
}
}
导出常量mapDispatchToProps=(调度)=>{
返回{
}
}
导出默认连接(mapStateToProps,mapDispatchToProps)(DisconnectedAllDogs)
我认为第二个组件(DogCard)上的一个变体(如下)有望解决您可能面临的问题
const DogCard = (props) => {
return (
<div class='dog-card'>
<Link key={ props.dog.id }>{ props.dog.name }</Link>
</div>
)
}
export default DogCard
const DogCard=(道具)=>{
返回(
{props.dog.name}
)
}
导出默认狗卡
关于另一个部分,我认为你做得很好。但如果您遇到问题,请随意添加构造函数,如下所示:
export class DisconnectedAllDogs extends React.Component {
constructor(props){
super(props);
this.state = null;
}
render() {
return (
<div>
<div className='all-dogs'>
<h1>Peter's Park</h1>
</div>
<div>
{ this.props.dogs.map(dog => {
return <DogCard dog = {dog}/>
})}
</div>
</div>
)
}
}
export const mapStateToProps = (state) => {
return {
dogs: state.dogs
}
}
export const mapDispatchToProps = (dispatch) => {
return {
}
}
export default connect(mapStateToProps, mapDispatchToProps)(DisconnectedAllDogs)
导出类DisconnectedAllDogs扩展React.Component{
建造师(道具){
超级(道具);
this.state=null;
}
render(){
返回(
彼得公园
{this.props.dogs.map(dog=>{
返回
})}
)
}
}
导出常量MapStateTops=(状态)=>{
返回{
狗:州。狗
}
}
导出常量mapDispatchToProps=(调度)=>{
返回{
}
}
导出默认连接(mapStateToProps、mapDispatchToProps)(断开连接AllDogs)
真的希望这会有帮助…当你
console.log
你的道具时会发生什么?正如@JulienD所说,试着console.log
你的道具,即你的DogCard
组件中的dog对象。而且,您已经定义了一个功能组件,因此您不需要使用这个。props
,只需使用props
。请告诉我ti是否有帮助,以便改进我的帮助答案