简体   繁体   English

如何使用 object 数组的 React-hooks 设置状态()

[英]How to setState() using React-hooks of array of object

How can I use React-hooks to setState() my array of objects so that I can display this in my editform to perform update action.如何使用React-hooks setState()我的objects array ,以便我可以在我的editform中显示它以执行update操作。

Any syggestion how can I do so with React-hooks .我怎么能用React-hooks做到这一点。

Github link for code Github 代码链接

//Blog.js //博客.js

class Blogs extends Component{
    constructor(props) {
        super(props);
            this.state = {
                modal: false,
                updatable: false,
                justClicked: null,
                activePage: 1,
                requiredItem : null,
                _id: '',
                blog_short_desc: '',
                blog_name: ''

            };
            this.replaceModalItem = this.replaceModalItem.bind(this);
            this.onTodoChange = this.onTodoChange.bind(this);
        }

    static propTypes = {
        getBlog: PropTypes.func.isRequired,
        deleteBlog: PropTypes.func.isRequired,
        updateBlog: PropTypes.func.isRequired,
        resume: PropTypes.object.isRequired,
        auth: PropTypes.object.isRequired,
        loading: PropTypes.object.isRequired
    }

    toggle = (id) => {
        this.setState({
            modal: !this.state.modal
        });
    }

    componentDidMount() {
      this.props.getBlog();
    }

    replaceModalItem(id) {
        this.setState({
          modal: true,
          requiredItem: id
        });
    }

    onTodoChange = (e) => {
        this.setState({ 
            [e.target.name] : e.target.value 
        });
    }

    onSubmit = (e, id) => {
        //Perform edit submit
    }

    render(){
        return(
            <Container>
            <div> 
                    {/* edit card dialog */}
                    <EditBlog blogs={blogs} user={this.props.auth} onTodoChange={this.onTodoChange}  {...this.state} toggle={this.toggle} onSubmit={this.onSubmit}/>

                    <Grid style={{padding: 0}} id="todo">
                        {/* Grid of display list of blogs */}
                    </Grid>
                </div>
            </Container>
        ) 
   }
}

const mapStateToProps = (state) => ({
    resume: state.resume,
    auth: state.auth,
    loading: state.apiCallsInProgress > 0
});

export default connect(mapStateToProps, {getBlog, deleteBlog, updateBlog }) (Blogs);

//Edit.js //编辑.js

const EditBlog = ({ blogs, toggle, onTodoChange, onSubmit, ...state}) => {
    const itemsPerPage = 6; 
    let activeBlogs = blogs.slice (itemsPerPage * state.activePage - itemsPerPage, itemsPerPage * state.activePage);
    return( 
        <span>
            {activeBlogs.map(({ _id, blog_short_desc, blog_name }) => (
            <Modal 
                isOpen = {state.modal && state.requiredItem === _id}
                toggle = {()=>this.toggle(_id)}    
            >
                <ModalHeader toggle={toggle}  style={{fontWeight: "bold"}}>
                    Edit your blog {state.blog_name}
                </ModalHeader>
                <ModalBody>
                    <Form onSubmit={e => onSubmit(e, state._id )}>
                        <FormGroup>
                            <Label for="blogHeading">Blog Heading</Label>
                            <Input type="text" name="blog_short_desc" id="blogHeading" placeholder="Update one liner"
                            onChange={onTodoChange} value={blog_short_desc}/>
                            <Label for="blogName">Blog Name</Label>
                            <Input type="text" name="blog_name" id="blogName" placeholder="Update blog name"
                            onChange={onTodoChange} value={blog_name}/>

                            <Button
                                color="dark"
                                style={{marginTop: '2rem'}}
                                block
                            >Edit blog</Button>
                        </FormGroup>
                </Form>
                </ModalBody>
            </Modal>
            ))}
        </span>

    )
}

const mapStateToProps = state => ({
    resume: state.resume,
    auth: state.auth
})

export default connect(mapStateToProps, { updateBlog })(EditBlog);

//JsonArray //JsonArray

[
    {
        "_id": "5ea2efeec26f173008dcb80b",
        "blog_short_desc": "Hey tammy tammy",
        "blog_name": "sirirrrheyyy siri",
        "blog_desc": "hello",
        "blog_image_link": "",
        "blog_by": "5e9478c5b53c4735d4a5cdf1",
        "blog_by_author": "Tanmoy Sarkar",
        "date": "2020-04-24T13:55:58.818Z",
        "__v": 0
    },
    {
        "_id": "5e9f07d0c25ee11fc0a1260f",
        "blog_short_desc": "Hey tammy",
        "blog_name": "sirirrrheyyy siri",
        "blog_desc": "fsfsfdfdsgdfgfdgdg",
        "blog_image_link": "",
        "blog_by": "5e9478c5b53c4735d4a5cdf1",
        "blog_by_author": "Tanmoy Sarkar",
        "date": "2020-04-21T14:48:48.633Z",
        "__v": 0
    }
]

//Redux //还原

在此处输入图像描述

//UI //用户界面

在此处输入图像描述

You questions is rather vague.你的问题比较模糊。 A little more clarification can help.多一点澄清会有所帮助。 I suppose you mean storing the son array in a state using the useState hook.我想您的意思是使用 useState 挂钩将儿子数组存储在 state 中。

 const [array, setArray] = useState(null)
 setArray(jsonArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React-hooks 中创建计时器 - How to create timer in React-hooks 使用 setState 将空数组设置为 object 属性时出错(反应 Hooks) - Error when using setState to set an empty array to object property ( react Hooks) React Hooks 数组中元素的 setState - React Hooks setState of an element in an array 如何在Jest和react-testing-library中使用react-hook - How to use react-hooks with Jest and react-testing-library 使用 React-Hooks 单击按钮后如何显示不同的组件? - How to display different component after button click using React-Hooks? React - 如何在 React-Hooks 中隐藏其他元素时显示一个元素 - React - How to show an element while hiding the others in React-Hooks 使用React-Hooks / Axios来获取数据并显示在表格中 - Using React-Hooks/Axios to fetch data and display in a table 使用 redux 与 react-hooks 没有连接 HOC - Using redux with react-hooks without the connect HOC 使用React-Hooks,如果其中一个兄弟姐妹更改了状态,如何防止从Array.map创建的组件重新呈现? - Using React-Hooks, how do I prevent a component created from Array.map from re-rendering if one of it's siblings changes state? React-Hooks:如何调用 useState 返回的 function - React-Hooks: How to call a function returned by useState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM