尽管这可能不是一个好的模式,但我对如何将单个React组件连接到商店感兴趣。
例如,如果我有:
const store = createStore();
const testComponent = props => {
const name = store.name; // this won't work because it won't update
return (<p>{ name }</p>);
};
我猜测我需要以某种方式通过props从商店传递值,以使组件自动更新。 connect
不过似乎似乎要求商店已经通过提供的内容注入了上下文。
我可以相对容易地创建一个附加组件作为该组件的父组件,但是我很想知道这是怎么可能的。
答案 0 :(得分:1)
如果您不想将组件包装在.container {
width: 500px;
max-width: 500px;
height: 200px;
overflow: hidden;
}
#slider {
width: 300%;
position: relative;
overflow: hidden;
animation: 6s slide infinite;
}
@keyframes slide {
0% {margin-left: 0}
16.67% {margin-left: 0}
33.33% {margin-left: -100%}
50% {margin-left: -100%}
66% {margin-left: -100%}
75% {margin-left: -200%}
100% {margin-left: -200%}
}
.box {
width: 33.33%;
height: 100%;
float: left;
}
.box div {
margin: 0 auto;
width: 50%;
height: 50px;
}
.one {
background: green;
}
.two {
background: red;
}
.three {
background: blue;
}
中,则可以使用redux中的connect
钩子,其工作方式如下:
useSelector
但是您仍然需要通过提供商注入商店。