在React/Redux中逐个在屏幕上显示内容,可以通过以下步骤实现:
constructor(props) {
super(props);
this.state = {
content: []
};
}
render() {
return (
<div>
{this.state.content.map((item, index) => (
<div key={index}>{item}</div>
))}
<button onClick={this.showNextContent}>显示下一个</button>
</div>
);
}
showNextContent = () => {
const { content } = this.state;
const newContent = [...content, '新的内容'];
this.setState({ content: newContent });
this.props.dispatch({ type: 'UPDATE_CONTENT', payload: newContent });
}
const initialState = {
content: []
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'UPDATE_CONTENT':
return { ...state, content: action.payload };
default:
return state;
}
};
import { createStore } from 'redux';
import { Provider } from 'react-redux';
const store = createStore(reducer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
通过以上步骤,你可以在React/Redux中逐个在屏幕上显示内容。每次点击按钮,都会添加一个新的内容到内容数组中,并更新显示。这种方式可以用于展示一系列的信息、步骤、图片等。
领取专属 10元无门槛券
手把手带您无忧上云