首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从带有搜索栏的道具中使用get衍生品执行状态更新

无法从带有搜索栏的道具中使用get衍生品执行状态更新
EN

Stack Overflow用户
提问于 2022-06-28 20:30:40
回答 1查看 33关注 0票数 0

我只是想知道你能不能帮我这个忙。我想从道具中更新状态,使用get衍生工具。但问题是,我在这个文件中也有一个搜索栏,它是用当地的州来做研究的。我的道具是用redux更新的,但我不能更新状态。而且,由于不推荐使用componentWillReceiveProps,因此不建议使用它。

下面是我代码的一小部分:

代码语言:javascript
复制
class modification extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            cle: this.props.cle,
            nom_shotgun: this.props.nom_shotgun,
            date_shotgun: this.props.date_shotgun,
            photo_shotgun: this.props.photo_shotgun,
            etat: this.props.etat,
            admin: this.props.admin,
            error: null,
            searchValue: "",
            data: this.props.data,
            loading: false,
        };
        this.arrayholder = this.props.data;
    }

    searchFunction = (text) => {
        const updatedData = this.arrayholder.filter((item) => {
            const item_data = `${item.nom_shotgun.toUpperCase()})`;
            const text_data = text.toUpperCase();
            return item_data.indexOf(text_data) > -1;
        });
        this.setState({ data: updatedData, searchValue: text });

    };

    static getDerivedStateFromProps(props, state) {
        if (props.data !== state.data) {
            return { data: props.data };
        }
        return null
    }

    render() {
        const Item = ({ nom_shotgun, cle, photo_shotgun, date_shotgun, etat, admin }) => {
            const handlerLongClick = () => {

                Alert.alert(
                    "Alert",
                    "Supprimer le shotgun?",
                    [
                        { text: "oui", onPress: () => this._supprimer(cle) },
                        { text: "non", onPress: () => console.log("no Pressed") }
                    ]
                );
            };
        }

        const renderItem = ({ item }) => <Item
          nom_shotgun={item.nom_shotgun}
          cle={item.cle}
          photo_shotgun={item.photo_shotgun}
          date_shotgun={item.date_shotgun}
          etat={item.etat}
          admin={item.admin}
        />;
        return (
            <View>
                <TouchableOpacity style={styles.item} onPress={() => this._navigation(etat, admin)} onLongPress={handlerLongClick}>
                    <View>
                        <Text style={styles.nom_shotgun}>{nom_shotgun}</Text>
                        <Text style={styles.date_shotgun}>{date_shotgun}</Text>
                        {this._admin(admin)}
                        <Text style={styles.etat}> {etat} </Text>
                        <Image
                            style={styles.photo_shotgun}
                            source={require('../image/shotgun2.png')}
                        />
                        <Divider style={{ height: 1 }} />
                    </View>
                </TouchableOpacity>
            </View>
        );
    };
}

欢迎任何建议,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-06-30 13:15:17

函数getDerivedStateFromProps不工作吗?

也许用shouldComponentUpdate

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72792831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档